Convert string into uppercase & lowercase - C

Write a C program to convert a given string into uppercase & vice versa.

Solution:

#include<stdio.h>
#include<string.h>
int main()
{
    char str[20];
    int i;
    printf("Enter String : ");
    scanf("%s",str);
    printf("----------------------------------\n");
    /* To print string in lowercase*/
    for(i=0;i<=strlen(str);i++)
    {
        if(str[i]>=65&&str[i]<=90)
        str[i]=str[i]+32;
    }
    printf("String in Lowercase: %s",str);
    /* To print string in upperCase*/
    for(i=0;i<=strlen(str);i++)
    {
        if(str[i]>=97&&str[i]<=122)
            str[i]=str[i]-32;
    }
    printf("\n\nString in Uppercase: %s",str);
    return 0;
}


Output:

lower upper

Note - You can do this program using library functions strupr() and strlwr(). But some compilers may not support these functions.

Q. Convert string into uppercase & lowercase using character function.

Solution:

In this program, the string is converting into upper and lower case using character function toupper and tolower. To use this function in program, it is mandatory to use header file ctype.h.

#include<stdio.h>
#include<string.h>
#include<ctype.h>
int main()
{
    char s[20];
    int i;
    printf("Enter String : ");
    gets(s);
    for(i=0; s[i]!='\0';i++)
        s[i]=toupper(s[i]);
    printf("---------------------------------\n");
    printf("String in Uppercase : %s\n",s);
    for(i=0; s[i]!='\0';i++)
        s[i]=tolower(s[i]);
    printf("\nString in Lowercase : %s",s);
    return 0;
}


Output:

lowercase uppercase