case conversion to lowercase even on using strupr

269 Views Asked by At

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.

1

There are 1 best solutions below

2
On

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];
}