tags:

views:

77

answers:

3

When two vowels come one after another then the count should increment.But i dont know why its incrementing it more than that.

#include<stdio.h>
#include<conio.h>
void main(void)
{       
    int i,j,count=0;
    char string[80];
    printf("Enter a string:\n");
    gets(string);
    for(i=0; ;i++)
    {
        if(string[i]=='\0')
            break;
        if(string[i]=='a'||string[i]=='A'||string[i]=='e'||string[i]=='E'||string[i]=='i'||string[i]=='I'||string[i]=='o'||string[i]=='O'||string[i]=='u'||string[i]=='U')
        {
            if(string[i+1]=='a'||string[i]=='A'||string[i]=='e'||string[i]=='E'||string[i]=='i'||string[i]=='I'||string[i]=='o'||string[i]=='O'||string[i]=='u'||string[i]=='U')
            count++;
        }
    }
    printf("%d",count);
    getch();
}
+5  A: 

Looks like you have a typo here, your code:

if(string[i]=='a'||string[i]=='A'||string[i]=='e'||string[i]=='E'||string[i]=='i'||string[i]=='I'||string[i]=='o'||string[i]=='O'||string[i]=='u'||string[i]=='U')
{
    if(string[i+1]=='a'||string[i]=='A'||string[i]=='e'||string[i]=='E'||string[i]=='i'||string[i]=='I'||string[i]=='o'||string[i]=='O'||string[i]=='u'||string[i]=='U')
        count++;
}

Notice the second if statement there, everything except the first condition is checking string[i] instead of string[i+1]. So if you have 'A' in string[i], then this will increment count no matter what is in string[i+1].

You want:

if(string[i]=='a'||string[i]=='A'||string[i]=='e'||string[i]=='E'||string[i]=='i'||string[i]=='I'||string[i]=='o'||string[i]=='O'||string[i]=='u'||string[i]=='U')
{
    if(string[i+1]=='a'||string[i+1]=='A'||string[i+1]=='e'||string[i+1]=='E'||string[i+1]=='i'||string[i+1]=='I'||string[i+1]=='o'||string[i+1]=='O'||string[i+1]=='u'||string[i+1]=='U')
        count++;
}

I also recommend you look up the function tolower which will lower-case a character, meaning you need to do less comparisons which will make this code much easier to read and maintain. Also you might consider using a switch or any array hereand probably writing a helper function.

I guess I just can't stand this code as it is, here's a better version:

int is_vowel(char ch)
{
    switch (tolower(ch))
    {
    case 'a': case 'e': case 'i': case 'o': case 'u':
        return 1;
    default:
        return 0;
    }
}

And then make your if statement:

if (is_vowel(string[i]) && is_vowel(string[i+1]))
    count++;

See, much cleaner and easier to read, don't you think?

SoapBox
the_void
@the_void: The `tolower` isn't even necessary if you're going to use `strchr()` - just use `"AaEeIiOoUu"` as the search string.
caf
@caf: Yep, you're right!
the_void
@Above: I read that strchr finds the first occurence of the entered character in the string.But you all are using "AaEeIiOoUu" (More than one characters) How is it going to work?
fahad
+2  A: 

You also have a buffer overflow:

gets(string);

And the following is bad style:

for(i=0; ;i++)
    {
        if(string[i]=='\0')
            break;

should be

for(i=0; string[i]!='\0';i++)
Chaos
I just find it easy to understand :)
fahad
A: 

For once, be as lazy as you can. If you have to repeat a portion of code, ask yourself if you can't do it without repeating.

I would have gone for that :

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

const size_t MAX_LENGTH = 100;

//Counts the occurrences of two consecutive same characters
//in a string, without case
size_t cnt_doubled(char const * const str, int c) {
    size_t ret = 0;

    //A pointer browses the string until terminating char and
    //increments ret if the pointed char is the one seeked
    //two chars in a row
    for(char const *p = str ; *p != '\0' ; ++p) {
        ret += tolower(*p) == tolower(c) && tolower(*(p+1)) == tolower(c);
    }

    return ret;
}

//Explicit...
size_t cnt_doubled_vowels(char *str) {
    char const *vowels = "aeiouy";

    //A pointer browses the vowels and takes into account
    //the occurrences in the string of every char pointed
    size_t n_vowels = 0;
    for(char const *p = vowels ; *p != '\0' ; ++p) {
        n_vowels += cnt_doubled(str, *p);
    }

    return n_vowels;
}

int main(void) {
    //fgets returns a string terminated by a newline char (before
    //terminating char of course) but in your case it doesn't
    //matter
    char string[MAX_LENGTH] = "";
    fgets(string, MAX_LENGTH, stdin);

    printf("N : %d", cnt_doubled_vowels(string));

    return 0;
}
Mister Mystère
Hem, why did I get a down vote ? At least comment to say why.
Mister Mystère