Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

How do we find the upper case and convert it into lower case in C? What I want t

ID: 3843069 • Letter: H

Question

How do we find the upper case and convert it into lower case in C?

What I want to do is to read from the standard input and output the words.

But if there exisits an upper case, it needs to be converted into _something.

For example, if helloWorld is inputted, it needs to be converted to, hello_world.

And standard input can be various words, identifying the words by space.

For example, we can input helloWorld applePie lululala and ends inputting if we enter "Enter"

then it should print out

hello_world

apple_pie

lululala

Is there any experts to solve this problem!!?? Thanks...

Explanation / Answer

You have to make use of the ascii values of upper case and lower case. The ascii value of upper case english letters ranges from 65-90 and lower case english letters ranges from 97-122 . Refer http://ee.hawaii.edu/~tep/EE160/Book/chap4/subsection2.1.1.1.html to see ascii value of various characters.

So the approach of the program would be something like this:

You have to iterate throught the given string and if you find a character whose ascii value(found by type-casting char to int) is between 65-90 (both inclusive) then its an upper case and to convert it to lower case add 32 to it. This is because ascii value of 'A' is 65, so if you add 32 you get 65+32=97, which is ascii value of 'a' .So adding 32 gives you lower case of that character.

Below is the C code for the above problem statement:

  #include<stdio.h>

int main(){
    char input[100]; //for simplicity I have set max limit to 100
    char ch;
    int i=0;
while(ch != ' ') // terminates if user hits enter
{
ch = getchar();
input[i] = ch;
i++;
}
input[i] = ''; //a string must be terminated by null character /0;
   
    int length = i; // we have length of our input

    int k=0;
        char temp[length]; //any word can be maximum of length of input, as all words are afterall subsets of the input string
    for( i=0;i<length;i++){
       
        if(input[i]==' '){ // if encountered by a space,print the new word stored in temp and make k=0 so that other words can be stored in temp starting from index 0
                temp[k]=''; // terminate the word by null character
                printf("%s ",temp);
                k=0;
                continue;
           }
           
            if((int)input[i]>=65&&(int)input[i]<=90){
                temp[k++]='_'; // as per question, if capital letter must be preceeded by '_' (underscore)
                temp[k++]=(int)input[i]+32; // converting upper case to lower case
           }else {
              temp[k++]=input[i];
           }
             
             
   }
      temp[k]='';
                printf("%s",temp); // prints the last word.
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote