Thread: sequence of vowels in a string

  1. #1
    Registered User
    Join Date
    Feb 2006
    Location
    Philadelphia, PA
    Posts
    27

    sequence of vowels in a string

    I am trying to code some sample practice problems for my C class but I ran into a segmentation fault error when trying to solve the following problem:

    Write a function which is passed a c-string and returns the length of the longest subsequence of vowels contained in the string. Ex. if string is "AUOZZIE" function should return 3.

    Attached is my code. Can anyone see a problem with it? Thank you in advance.
    Code:
    #include <stdio.h>
    #include <ctype.h>
    int vowel(char[]);
    
    int main(int argc, char **argv)
    {
    
      char word[] = "AUOZKKUU";
    
      printf("%d", vowel(word));
      printf("\n");
    
      return 0;
    }
    
    int vowel(char s[]){
        int i=0;
        int count=0;
        int countMax=0;
        int inst=0;
        while(s[i]!='\0'){
            START:
            if(s[i]=='A' || s[i]=='E' || s[i]=='I' || s[i]=='O' || s[i]=='U' || s[i]=='Y'){
                count++;
                i++;
                goto IS_NEXT_VOWEL;
            }
            IS_NEXT_VOWEL:
            while(s[i]=='A' || s[i]=='E' || s[i]=='I' || s[i]=='O' || s[i]=='U' || s[i]=='Y'){
                count++;
                i++;
            }
            i++;
            if(count>countMax)
                countMax=count;
            count=0;
            goto START;
    
        }
        return countMax;
    }

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,337
    First, you don't need a single GOTO or LABEL. If you removed them from your source, the program flow would not change one iota.

    Second, you don't need two IF clauses checking for a vowel. All you need is one, and the one you have written are fine.
    Mainframe assembler programmer by trade. C coder when I can.

  3. #3
    Registered User
    Join Date
    Feb 2006
    Location
    Philadelphia, PA
    Posts
    27
    Problem fixed. Thanks for a quick reply!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Please check my C++
    By csonx_p in forum C++ Programming
    Replies: 263
    Last Post: 07-24-2008, 09:20 AM
  2. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 04:13 PM
  3. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM