Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Design a C program to convert a string into uppercase as well as lowercase without using in built string function.

+7 votes
asked Dec 24, 2021 by Ritik Joshi (190 points)
#include <stdio.h>  
#include <conio.h>  
int main ()  
    char str1[30], str2[30]; // declare variables  
    int i=0;  
    // convert in lower case  
    printf (" Enter the Upper Case Character: ");  
    scanf (" %[^\n]", str1);
    printf (" \n Enter the Lower Case Character: ");  
    scanf (" %[^\n]", str2);
       { if(str1[i]>64 && str1[i]<91 )
            str1[i]=str1[i] + 32;
        {if(str2[i]>97 && str2[i]<122 )
            str2[i]=str2[i] - 32;
    } while(str1[i]!='\0');
    printf ("  \ncharacter in Lower case is: %s", str1);  
    printf ("  \ncharacter in the Upper case is: %s", str2);  
    return 0;  

what is the problem with this code??????????????

1 Answer

+1 vote
answered Dec 28, 2021 by Peter Minarik (55,660 points)

Problem #1: Double increment

Let's consider your loop:

        if (str1[i] > 64 && str1[i] < 91)
            str1[i] = str1[i] + 32;
        i++; // i is incremented here first

        if (str2[i] > 97 && str2[i] < 122)
            str2[i] = str2[i] - 32;
        i++; // i is incremented here second
} while (str1[i] != '\0');

If you pay close attention, you can see that in a single loop iteration, you increment the loop variable i twice. This makes your function skip every even index (2, 4, 6, ...) in the lower case transformation and every odd index (1, 3, 5, ...) in the upper case transformation. Keep in mind that indexing is 0 based.

Problem #2: Lower case 'a' and 'z' not transformed

You are not transforming lower case 'a' (97) and lower case 'z' (122) to their upper case version due to your condition, as it only converts characters between 'a' and 'z', but not the 'a' and 'z' that form the boundaries (greater and lesser conditions):

if (str2[i] > 97 && str2[i] < 122)

Problem #3: Don't assume both inputs to be the same length

Your code assumes that the tho input strings str1 and str2 are of the same length. This could lead to an erroneous behaviour.

Suggestion #1: Keep your code readable

I'd highly recommend keeping your code readable. Do not use character codes, that means nothing, instead use the characters themselves. So instead of 97, use 'a', instead of 122 use 'z', etc.

Suggestion #2: Keep your includes minimalist

Do not add #includes that you do not need (<conio.h> is not used).

A proposed improvement

Fixing the problems above and applying my suggestions, your code would look something like this:

#include <stdio.h>

int main()
    char str1[30];
    char str2[30];

    printf("Enter the upper case string: ");
    scanf(" %[^\n]", str1);
    printf("Enter the lower case string: ");
    scanf(" %[^\n]", str2);
    char toLowerOffset = 'a' - 'A';
    char toUpperOffset = 'A' - 'a';

    for (int i = 0; str1[i] != '\0'; i++)
        if (str1[i] >= 'A' && str1[i] <= 'Z')
            str1[i] += toLowerOffset;

    for (int i = 0; str2[i] != '\0'; i++)
        if (str2[i] >= 'a' && str2[i] <= 'z')
            str2[i] += toUpperOffset;

    printf("Upper case string converted to lower case: %s\n", str1);
    printf("Lowser case string converted to uper case: %s\n", str2);
    return 0;
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.