extraction

This is a discussion on extraction within the C Programming forums, part of the General Programming Boards category; Beginner C I am trying to extract "aeiou" vowels from line that the user input ex: user input: dghrywei I ...

  1. #1
    Max
    Max is offline
    Registered User Max's Avatar
    Join Date
    Jul 2002
    Posts
    110

    extraction

    Beginner C

    I am trying to extract "aeiou" vowels from line that the user input

    ex: user input: dghrywei

    I want to write a program that can print only the vowels

    This is what I have so far...can't get it to work!!!

    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    #include <string.h>
    int main(void)
    {
    char line[80];
    char string[80];
    char vowels[80];
    char consonates[80];
    int k;

    k=0;
    printf("Input characters: ");
    gets(line);

    strcpy(string, line);

    while (strlen(string[k]))
    if (string[k]=='a'||'e'||'i'||'o'||'u') printf("%d\n", k);

    return 0;
    }

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Just by glancing over it , you never increment your k variable in the while loop. I don't know if that will fix all of your problems, but it is a start.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  3. #3
    Registered User
    Join Date
    Jul 2002
    Posts
    28
    As i've learned from this forum you should use "fgets" instead of "gets".

    You have the right idea but it can be done with a lot less code(work).

    Here's some streamlined code that will do what you want. If you have questions post another thread.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
     int n=0;
     char input[BUFSIZ];
    
     printf("Input characters: "); 
     if(fgets(input,BUFSIZ,stdin)!=NULL)
     {
      sscanf(input, "%s", input);
     }
    
     // parse the string printing vowels only
     while(input[n++]!='\0')
     {
      switch(input[n])
      {
       case 'a': case 'e': case 'i': case 'o': case 'u':
       printf("%c", input[n]);
       break; 
    
       default:
       break; 
      } // end switch(input[n])
     } // end while(input[n]!='\0')
    
     return EXIT_SUCCESS;
    } // end int main(void)
    Last edited by jerryvtts; 07-15-2002 at 12:25 AM.

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,655
    Code:
    
    char *strip_consonants(char *input)
     {
       if( input == NULL) return input; //...return to sender ;)
      
       int len = strlen(input), i, t;
    
       char temp[len];
    
       strcpy(temp, input);
    
       for(i = 0, t = 0; t < len; t++)
         switch( tolower(temp[t]) )
          {
             case 'a': 
             case 'e': 
             case  'i': 
             case 'o': 
             case 'u': 
             input[i++] = temp[t];
             break;        
          }
    
         input[i] = 0;
    
    return input;
     }
    
    
    
    int main(){
    
    char answer[] = "Answer is found";
    
    printf( "Answer: %s", strip_consonants(answer) );
    
    getch();
    
    return 0;
    }

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,439
    > string[k]=='a'||'e'||'i'||'o'||'u'
    This would need to be written as

    string[k]=='a' || string[k] == 'e' || string[k] == 'i' || string[k] == 'o' || string[k] == 'u'

    > sscanf(input, "%s", input);
    This is both unnecessary and dangerous (specifically undefined)
    You can't use the same buffer for input and output when using sprintf and sscanf

    > If you have questions post another thread
    Better if you replied to this one

    I can't see why you would need to store any character in a buffer anyway

    Code:
    int ch;
    while ( (ch=getchar()) != EOF ) {
      if ( ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' ) {
        // it's a vowel
        putchar( ch );
      }
    }

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,439
    > char temp[len];
    C'mon Sebastiani, stop with the C++ (or one of the few with a C99 compiler)

    It's also a bug - it's one char short of the required length - you forgot the '\0'

  7. #7
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,655
    > sscanf(input, "%s", input);
    Good call. And you know, I just plain hate scanf. I realize sscanf is prolly more reliable with strings, but scanf is simply buggy! I always prefer fgets(). I'm done

    > string[k]=='a'||'e'||'i'||'o'||'u'
    That seemed wrong to me too, but somewhere in the back of my mind I remember a variation similar to the above syntax. Am I delusional?

  8. #8
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,655
    Geez, you'd think I'd stop doing that, sorry. But hey, no offense Salem, but I think YOU are in the minority insofar as compilers are concerned! I am tempted to take a poll! Salem, C++ is here to stay. The line between the two is blurring! Let's enjoy it's wonderful features - eh?

  9. #9
    Registered User
    Join Date
    Jul 2002
    Posts
    28
    Note that sscanf isn't an ordinary method or built-in function, but a very special construction in Pike. The reason is that sscanf has to change its arguments, and since Pike uses call-by-value, no ordinary method or built-in function can do that.

    Certainly in this case it may not be necessary but I use it because
    it always guarantees a properly terminated string.

    But I don't mind the criticism, keeps me on my toes and as I've found out in this forum after 20 years of programming C I still have a lot to learn!
    Last edited by jerryvtts; 07-14-2002 at 02:45 AM.

  10. #10
    Banned borko_b's Avatar
    Join Date
    Jun 2002
    Location
    Well... I live in Bulgaria :)
    Posts
    100
    well try this one:
    Code:
    int strip_vowels(char *s_in,char *s_out, int iout_len) {
         char vowels[] = "aeiou";
         char *pin,*pout , *pv;
    
         pin = s_in;
         pout=s_out;
    
         while(*pin) {
             pv = vowels;
             while(*pv) {
                  if(*pin == *pv) {
                       *pout = *pv;
                       if((int)(pout - s_out -1)>iout_len) {
                            *pout = '\0';
                            return -1;//ERROR buffer is out of memory
                       }
                       pout++;
                       break;
                  }
                  pv++;
             }
             pin++;
         }
         *pout = '\0';
         return (int)(pout - s_out -1); /*return the lenght of the out*/
    }
    
    
    int main(void) {
        char str[] = "Some testing string";
        char out[200]; 
        strip_vowels(str,out,sizeof(out));
        printf("there are \"%s\" vowels in\n",out);
        printf("\"%s\" string\n",str);
        return 0;
    }
    i made late in the night
    and i didn't test it...
    Please let me know if it works OK...
    The choke point i think is
    in the buffer lenght test i think...

  11. #11
    Max
    Max is offline
    Registered User Max's Avatar
    Join Date
    Jul 2002
    Posts
    110
    Thank you all !!!!!!!!

    I need to be able to store the vowels in a string and the consonate in another string and print each string separetly

    how can i do that.....this is what I have so far

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    int main(void)
    {
    int n=0;
    char input[80];

    printf("Input characters: ");
    gets(input);

    while(input[n++]!='\0')
    {
    switch(input[n])
    {
    case 'a':
    case 'e':
    case 'i':
    case 'o':
    case 'u':

    printf("%c", input[n]);
    }
    }
    return 0;
    }

  12. #12
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Max, if you're going to ask for advice, best you listen a little harder to it. Look:
    Originally posted by jerryvtts
    As i've learned from this forum you should use "fgets" instead of "gets".
    So come on, stop using gets() in your code, it's not good!

    Now if you really want to add things to a string (char array), you could use strncat(), or you could write directly to each byte. Just make sure not to go past the end of the buffer.

    Also, this isn't going anywhere:
    >while(input[n++]!='\0')
    >{
    >switch(input[n])
    You incremented n on the while line, then used it's new value on the switch line. So at the start of the execution, the while changes n from 0 to 1, and the switch never sees n as being 0.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  13. #13
    Registered User
    Join Date
    Jul 2002
    Posts
    28
    Also, this isn't going anywhere:
    >while(input[n++]!='\0')
    >{
    >switch(input[n])

    Hammer. this was my fault. It should have been:

    Code:
    while(input[n]!='\0') 
    { 
    switch(input[n++])

  14. #14
    Banned borko_b's Avatar
    Join Date
    Jun 2002
    Location
    Well... I live in Bulgaria :)
    Posts
    100
    >>...I want to write a program that can print only the vowels ...

    !!!!
    Did you tried any of the code posted here???

    Your code didn't change to much

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. CSS extraction
    By anandganapati in forum C Programming
    Replies: 1
    Last Post: 04-29-2008, 11:26 PM
  2. Video Capture/Frame Extraction apis?
    By maththeorylvr in forum C Programming
    Replies: 1
    Last Post: 03-13-2006, 08:07 PM
  3. String extraction
    By ren in forum C Programming
    Replies: 2
    Last Post: 04-08-2002, 10:39 AM
  4. char extraction
    By Kohatian 3279 in forum C++ Programming
    Replies: 6
    Last Post: 03-28-2002, 03:03 PM
  5. Text extraction
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 02-27-2002, 03:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21