-4

I don't know how to PRINT the longest substring of vowels. I know to count and find length of the longest substring, but I don't know how to print. This is example: Input: 12 fg aaa bvcf ssd ae Output: aaa

    ```
    int isVowel(char c) {
    char vowels[] = { "aeiouAEIOU" };
    int length = strlen(vowels);
    int i;
    for (i = 0; i < length; i++) {
    if (c == vowels[i]) return 1;
    }
    return 0;
    }

    void theLongestSubstring(char s[]) {
    int length = strlen(s);
    int i, j;
    char newString[100];
    int br = 0;
    int count;
    int maxCount = 0;
    for (i = 0; i < (length - 1); i++) {
    count = 1;
    if (isVowel(s[i]) == 1) {
        for (j = i + 1; j < length; j++) {
            if (isVowel(s[j]) == 1) {
                count++;
            }
            else {
                break;
            }
         }
       }
     }
    if (count > maxCount) {
    maxCount = count;
     }
    //newString[br] = '\0';
    printf("Length of the longest substring of vowels is:\n*** %d ***\n", 
    maxCount);
    }
    ```
Milica Simic
  • 11
  • 1
  • 3

1 Answers1

0

It is very similar to the counting of the longest vowel substring.

First, you will need two more arrays(for storing current sequence of vowels and longest overall string) and an int value(for being able to go trough arrays independently of the "for loop").

char maxString[100] = "", currentString[100];
int count = 0, maxCount = 0;
int k = 0;

Second, you dont need two nested for loops for this, one is enough.Just check if the current char is vowel.If it is increase the "count" int variable by one and store it inside the "currentString", if it is not check if the "count" value is bigger than "maxCount" value and if it is set the "maxString" equal to "currentString" and than set the "count" value back to 0, so next sequence can be checked in next loop when first next vowel is found.

inside of "i" for loop, if it is a vowel:

        count++;
        currentString[k++] = s[i];

else if it is not a vowel:

if (count > maxCount) {
            strcpy(maxString, currentString);
            maxCount = count;
        }
        count = 0;
        k = 0;
        strcpy(currentString, "");
    }

This should probably help a bit, i didnt give you whole solution so you can try to do it by yourself.

Also your "printf" function is outside of the for loop and wont print the max value.If the "count" is greater than the "maxCount" should be checked at the end of the for loop.And an advice would be to think about not using the for loop for checking if the char is a vowel, there is a more efficient way using ascii.

mrK
  • 1
  • 2