-1

Q : Write a program to count the number of occurrences of any two vowels in succession in a line of text. For example the following sentence :

"Please read the application and give me  gratuity"

Such occurrences in the sentence are :- "ea" , "ea" , "io" , "ui". Ultimately the question is to count the number of such occerrences in the line which is a user input string.

Problem : My program is just recieve a line but did not give any output.

It's my first question in stackoverflow. I am a beginner in programming.

My code:

# include <stdio.h>

int main () {
    char line[100];
    printf("\nEnter a line\n");
    gets(line);
    //printf("You entered : %s\n", line);
    char A,E,I,O,U,a,e,J,o,u;
    A = 'A';
    E = 'E';
    I = 'I';
    O = 'O';
    U = 'U';
    a = 'a';
    e = 'e';
    J = 'i';
    o = 'o';
    u = 'u';
    int occurence =0,i =0 ;
    while (line[i] =! '\0'){
        if((line[i] == A || E || I || O || U || a || e || J || o || u) && (line[i+1] == a || e || J || o || u)){
                    occurence++;
        }
        i++;
    }
    printf("Number of occurence of any two vowels in succession in the line is : %d\n", occurence);
    return 0;
}

outputimage

anastaciu
  • 23,467
  • 7
  • 28
  • 53
  • 2
    `line[i] == A || E || I || O || U || a || e || J || o || u` won't do what you think it does. You need to do `line[i] == A || line[i] == E ...`. (Also, any reason why you chose to store each different vowel with each different case in its own variable? Why not just use normal literals?). You could also cut down on the size of this by doing `toupper(line[i]) == A || toupper(line[i]) == E ...` to do case-insensitive checking. – mediocrevegetable1 Apr 20 '21 at 11:00
  • @mediocrevegetable1 But that is also not working. – Debabrata Jana Apr 20 '21 at 11:49
  • `line[i] =! '\0'` *also* doesn't do what you think it does. The correct operator is `!=`. – mediocrevegetable1 Apr 20 '21 at 12:24

2 Answers2

0

Your code has multiple issues, some of which are:

  • You use gets to read the string. This function is now at least deprecated (I think it was even removed from recent versions of the standard library) because it is unsafe. Use fgets instead!
  • You use the || operator wrong - this was already pointed out in mediocrevegetable1's comment.

You could use a code similar to this one to solve your problem. The code contains comments, so it should be easy to understand. However, if this is a homework or project for school, do NOT use this exact code, as this would most likely be considered plagiarism!

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

#define STRLEN 100

int main () {
    char line[STRLEN];
    char* ch;
    char incrementIfVowel;
    int occurences;

    /* Read line */
    printf("\nEnter a line\n");
    fgets(line, STRLEN, stdin);

    /* Init variables */
    incrementIfVowel = 0;
    occurences = 0;

    /* Iterate through characters */
    for (ch = line; *ch != '\0'; ch++) {
        /* Test if the current character is a vowel. Three cases can occur: */
        if (toupper(*ch) == 'A' || toupper(*ch) == 'E' || toupper(*ch) == 'I' || toupper(*ch) == 'O' || toupper(*ch) == 'U') {
            if (incrementIfVowel == 1) {
                /* Case 1: The current character is a vowel, and its predecessor was also a vowel */
                incrementIfVowel = 0;
                occurences++;
            }
            else {
                /* Case 2: The current character is a vowel, but its predecessor was not a vowel or it was the second vowel in a row */
                incrementIfVowel = 1;
            }
        }
        else {
            /* Case 3: The current character is not a vowel */
            incrementIfVowel = 0;
        }
    }

    /* Print result */
    printf("Number of occurence of any two vowels in succession in the line is : %d\n", occurences);

    return 0;
}
Xaver
  • 1,035
  • 9
  • 17
0

There are 3 main issues with your code:

gets(line);

gets doesn't check the length of the buffer and for this reason, is susceptible to buffer overflows. gets had been deprecated since C99 and was removed in C11. You're probably compiling with an older version of the standard; I'd suggest you switch to newer versions. As for an alternative, see fgets.

while (line[i] =! '\0'){

=! is a typo. Replace it with !=

if((line[i] == A || E || I || O || U || a || e || J || o || u) && (line[i+1] == a || e || J || o || u))

This will always evaluate to true because || doesn't chain like that. Ideally, you should put this in a function:

_Bool is_vowel(char ch)
{
    return toupper(ch) == 'A' || toupper(ch) == 'E' || toupper(ch) == 'I' || toupper(ch) == 'O' || toupper(ch) == 'U';
}

toupper is defined in <ctype.h> so be sure to include that. You could also shorten this behemoth of a line with return strchr("AEIOUaeiou", ch), but if you haven't used strchr and are not comfortable with using it yet, that's okay.

Modifying only the incorrect parts, your final code will can look something like this:

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

_Bool is_vowel(char ch)
{
    return toupper(ch) == 'A' || toupper(ch) == 'E' || toupper(ch) == 'I' || toupper(ch) == 'O' || toupper(ch) == 'U';
}

int main () {
    char line[100];
    printf("\nEnter a line\n");
    fgets(line, sizeof line, stdin);
    
    int occurence = 0, i = 0;
    
    while (line[i] != '\0') {
        if(is_vowel(line[i]) && is_vowel(line[i + 1]))
            occurence++;
        i++;
    }
    printf("Number of occurence of any two vowels in succession in the line is : %d\n", occurence);
    return 0;
}

An example of running this:

Enter a line
Please do something about the bee hive and then eat some meat
Number of occurence of any two vowels in succession in the line is : 5

(5 because Pl<ea>se do something ab<ou>t the b<ee> hive and then <ea>t some m<ea>t)

mediocrevegetable1
  • 4,086
  • 1
  • 11
  • 33