简体   繁体   中英

stack with finding character inside string in C language

#include <stdio.h>
#include <string.h>

main()
{
  int i;
  int *b, *z;
  char name[30];
  char vowel[5] = {'A', 'E', 'I', 'O', 'U'};
  char consonants[23] = {'B','C','D','F','G','H','J','K','L','M','N','P','Q','R','S','T','V','W','X','Y','Z'};

  printf ("input the string: ");
  scanf  ("%s", name);
  printf ("The string is %s\n", name);

  for (i=0; name[i]!='\0'; i++){
    if
      (b=strchr(vowel, name[i]) != NULL) {
      printf ("The vowels are:  %s\n", b); }
    else if
      (z=strchr(consonants, name[i]) != NULL) {
      printf ("The consonants are:  %s\n", z);
    }
  }
}

I am trying to find how many vowels and consonants in array. That's the only algorithm that our teacher showed us, but it doesn't work. Any one can point me to my mistakes?

I just did one more try, with all your advices,

#include <stdio.h>
#include <string.h>

int main()
{
int vow, cons, i;
char *s, *s1;
char name[30];
char vowel[6] = "AEIOU";
char consonants[21] = "BCDFGHJKLMNPQRSTVWXYZ";

printf ("input the string: ");
scanf  ("%s", name);
printf ("The string is %s\n", name);
for (i=0; name[i]!='\0'; i++)
s = strchr(vowel, name[i]);
printf ("The vowels are:  %s\n", s);

s1 =strchr(consonants, name[i])) {
printf ("The consonants are:  %s\n", s1);
}

return 0;

}

This is how I changed it, with all your advices, what is my other problems? cause still dosen't work fine. Thanks.

And this is my another version of program

 #include <stdio.h>
 #include <string.h>

 int main()
 {
 int i;
 int counter=0, counter2=0;
 char *s;
 char name[30];
 char vowel[6] = "AEIOU";
 char consonants[21] = "BCDFGHJKLMNPQRSTVWXYZ";

 printf ("input the string: ");
 scanf  ("%s", name);
 printf ("The string is %s\n", name);
 for (i=0; name[i]!='\0'; i++) {
 if (s = strchr(vowel, name[i])) {
 counter++;
 }
 else if (s =strchr(consonants, name[i])) {
 counter2++;
 }
 printf ("First counter is %d\n", counter);
 printf ("The second counter is %d\n", counter2);
 return 0;
 }
  }

I added counters to count quantity of vowels and consonants, still doesn't work.

strchr() is for searching in strings.

char vowel[] = "AEIOU";
char consonants[] = "BCDFGHJKLMNPQRSTVWXYZ";
#include< stdio.h>
int main()
{
    int vowel=0,consonant=0;
    printf ("input the string: ");
    scanf  ("%s", name);
    printf ("The string is %s\n", name);
    for(int i=0;name[i] !='\0';i++)
    {
        if( name[i] == 'A' || name[i] == 'E' || name[i] == 'I' || name[i] == 'O' || name[i] == 'U' )    
        {
            vowel++;
        }
        else
            consanant++;
    }
    printf("%d %d",vowel,consonant);
    return 0;
}

When I compile this, I get the following messages:

$ gcc -Wall vc.c
vc.c:4:1: warning: return type defaults to ‘int’ [-Wreturn-type]
vc.c: In function ‘main’:
vc.c:17:8: warning: assignment makes pointer from integer without a cast [enabled by default]
vc.c:17:3: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
vc.c:18:4: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int *’ [-Wformat]
vc.c:20:13: warning: assignment makes pointer from integer without a cast [enabled by default]
vc.c:20:3: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
vc.c:21:4: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int *’ [-Wformat]
vc.c:24:1: warning: control reaches end of non-void function [-Wreturn-type]

So, start by making sure that your return type for main is 'int'

int main(){

and adding a return at the bottom of the function

return 0;

Thereafter, set b and z to be char *s, so that they match the return type of strchr

char *b, *z;

This will get rid of all the warnings.

$ gcc -Wall vc.c
$

Excellent. Now, when we run your program:

$ ./a.out 
input the string: aaa
The string is aaa
Segmentation fault

"Segmentation fault" means you're running off the end of an array and reading memory you don't own. Now implement Ignacio Vazquez-Abrams' solution

char vowel[] = "AEIOU";
char consonants[] = "BCDFGHJKLMNPQRSTVWXYZ";

Now your program will run to completion.

$ ./a.out 
input the string: AAA   
The string is AAA
The vowels are:  AEIOU
The vowels are:  AEIOU
The vowels are:  AEIOU

But it doesn't do much, does it?

So, if you're just trying to count how many vowels and consonants there are, you can just add an integer for each that increments every time the correct type is found and output them at the end:

printf("Vowels:\t%d\nConsonants:\t%d", vowelsFound, consonantsFound);

However, if you're trying to output them as lists, you're going to have much more data manipulation to do. Some links to check out:

Linux Man Page for printf

Linux Man Page for String functions

You have placed the return statement inside the for loop which is preventing it from scanning the entire name array.

When using strchr , you'll also need to convert the current loop character to uppercase for it to match properly since you have defined vowels in uppercase. To use toupper() you need to include ctype.h .

You also don't need to define consonants . What is not a vowel is a consonant.

Here's the code. I've tested it and it works:

#include <stdio.h>
#include <string.h>
#include <ctype.h>

int main()
{
    int i;
    int counter=0, counter2=0;
    char *s;
    char name[30];
    char vowel[6] = "AEIOU";

    printf ("input the string: ");
    scanf  ("%s", name);
    printf ("The string is %s\n", name);
    for (i=0; name[i]!='\0'; i++) {
        if (strchr(vowel, toupper(name[i])) != NULL) {
            counter++;
        }
        else {
            counter2++;
        }
    }
    printf ("First counter is %d\n", counter);
    printf ("The second counter is %d\n", counter2);
    return 0;
}

Alternatively.

#include <stdio.h>
#include <string.h>
int main() {
 int t [256];
 int i,c;
 int cntw = 0;
 int cntc = 0;
 const char * vowel="AEIOUaeiou";
 const char * consonants="BCDFGHJKLMNPQRSTVWXYZbcdfghjklmnpqrstvwxyz";
 memset(t,0,256);
 while (*vowel) { t[*vowel] = 1; ++vowel;}
 while (*consonants) { t[*consonants] = 2; ++consonants;}
 printf ("Input the text:  CTRL-D to end\n");
 c = getchar();
 while(c >=0) {
  switch(t[c]) {
   case 1: ++cntw; break;
   case 2: ++cntc; break;
  }
  c=getchar();
 }
 printf ("Text has %d vowel%s and %d consonant%s\n",
  cntw,(cntw>1)?"s":"",
  cntc,(cntc>1)?"s":"");
 return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM