0

I wrote this program to convert vowels to uppercase and consonants to lowercase for a given input.Once the string is converted to uppercase its being converted to lowercase but i haven't used strlwr.here is the code..

void main()
{
    char a[20], b[20], c[10] = "aeiou";
    int t, n;
    printf("enter the word");
    gets(a);
    strlwr(a);
    n = strlen(a);
    for(t = 0; t<n; t++)
    {
        if(a[t] == c[0] || a[t] == c[1])
        {
            strupr(a);
        }

        else if(a[t] == c[2] || a[t] == c[3])
        {
            strupr(a);
        }
        else if(a[t] == c[4])
        {
            strupr(a);
        }
        else
        {
            strlwr(a);
        }
        b[t] = a[t];
        /*line 456*/
    }
    b[n] = '\0';
    printf("%s", b);
}

consider the input aaasa. 1st a is con to upr,2nd a is con to lwr(since string is converted to upper in 1st loop and i haven't changed it to lowercase.),3rd a to upr, s to lwr, 4th a to upr. This is solved by placing strlwr(a) at line 456 but i want to know why uppercase is being converted to lowercase even though i haven't used strlwr anywhere in the if else blocks. please answer this.thank you in advance.

Barmak Shemirani
  • 30,904
  • 6
  • 40
  • 77
  • 2
    `strlwr/strupr` are not standard functions, and you don't even want them as you need to convert characters, not strings. Luckily there are standard functions for it - `toupper` and `tolower`. – Eugene Sh. Jul 19 '18 at 17:01
  • 3
    Don't use `gets`. Never ***ever*** use `gets`. It's a [dangerous function](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it-should-not-be-used) and has therefore been removed from the C language. Use e.g. [`fgets`](http://en.cppreference.com/w/c/io/fgets) instead. – Some programmer dude Jul 19 '18 at 17:01

1 Answers1

1

Instead of using strlwr/strupr, for changing individual characters, simply add/subtract 32 from the char for the respective operations.

So, if you consider the following line:

char x = 'A';

then,

x += 32;

would set the value of x to a.

EDIT

Your entire for loop would be:

for(t=0;t<n;t++)
{
    int i;
    int vowel = 0;
    for(i=0;i<5;i++)
    {
        if(a[t]==c[i])  //Lowercase vowels
        {
            a[t]-=32;
            vowel = 1;
            break;
        }
        else if((a[t]+32)==c[i])  //Uppercase vowels
        {
            vowel = 1;
            break;
        }
    }
    if(!vowel && a[t]<97)
        a[t]+=32;

    b[t]=a[t];
}
Susmit Agrawal
  • 3,649
  • 2
  • 13
  • 29