Program to convert first letter of each word of a string to uppercase and other to lowercase

Posted in

Program to convert first letter of each word of a string to uppercase and other to lowercase
vinaykhatri

Vinay Khatri
Last updated on April 19, 2024

    In this tutorial, we will write the logic in 3 different programming languages that will accept a string from the user and convert the first letter of each word to uppercase (Capital alphabet) and convert the rest of the letters of each word to lowercase (small letters).

    Algorithm

    The algorithm behind the program is quite straightforward. We will traverse through every character of the string and check for the following conditions.

    1. if we are on the first character, just convert it to uppercase.
    2. Else if check if the character is an empty space, if yes uppercase the next character value, and skip the next character by incrementing the traversing i pointer.
    3. Else, for the rest of the letters, just convert them into lowercase.

    1. C Program to convert the first letter of each word of a string to uppercase and other to lowercase

    #include <stdio.h>
    #include <string.h>
     
    int main() 
    {
     
      //Initializing variables.
      char str[100];
      int i,length = 0;
     
      //Accepting inputs.
      printf("Enter a Sentance:\n");
      gets(str);
      
      //calculate the length of the string
      length = strlen(str);
      
      for(i=0;i<length;i++)
      {	  // capitalize the first most letter
          if(i==0) 
          {
              str[i]=toupper(str[i]);
          }
          //check if the chracter is an empty space
          // capitalize the next chracter
          else if(str[i]==' ')
          {
              str[i+1]=toupper(str[i+1]); 
    		  i+=1;  // skip the next chracter
          }
          else
          {
          	 str[i]=tolower(str[i]); // lowercase reset of the chracters
    	  }
      }
      
      //Printing the sentance after changes.
      printf("Sentance after the Change: %s", str);
     
      return 0;
    }

    Output

    Enter a Sentance:
    thiS IS a GAME oF fiRe
    Sentance after the Change: This Is A Game Of Fire

    2. C++ Program to convert the first letter of each word of a string to uppercase and other to lowercase

    #include <iostream>
    #include <string.h>
    using namespace std;
     
    int main()
    {	
    	//Initializing variables.
      char str[100];
      int i,length = 0;
     
      //Accepting inputs.
      cout<<"Enter a Sentance:\n";
      gets(str);
      
      //calculate the length of the string
      length = strlen(str);
      
      for(i=0;i<length;i++)
      {	  // capitalize the first most letter
          if(i==0) 
          {
              str[i]=toupper(str[i]);
          }
          //check if the chracter is an empty space
          // capitalize the next chracter
          else if(str[i]==' ')
          {
              str[i+1]=toupper(str[i+1]); 
    		  i+=1;  // skip the next chracter
          }
          else
          {
          	 str[i]=tolower(str[i]); // lowercase reset of the chracters
    	  }
      }
      
      //Printing the sentance after changes.
      cout<<"Sentance after the Change: "<< str;
     
      return 0;
    }

    Output

    Enter a Sentance:
    thiS IS a GAME oF fiRe
    Sentance after the Change: This Is A Game Of Fire

    3. Python Program to convert the first letter of each word of a string to uppercase and other to lowercase

    string = input("Enter a Sentance:\n")
    new_string =""
    for i in range(len(string)):
        if i==0:
            new_string+= string[i].upper()
        elif string[i-1]==" ":
            new_string+=string[i].upper()
        else:
            new_string+= string[i].lower()
    
    print("Sentance after the Change: ",new_string)

    Output

    Enter a Sentance:
    thiS IS a GAME oF fiRe
    Sentance after the Change: This Is A Game Of Fire
    Note: "Python strings are immutable, once initialize, can not be changed  using assignment operator, that's why we have used an additional string to achieve our task".

    Complexity Analysis

    • Time Complexity: O(N), because we are traversing through every character of the string using a single for loop.
    • Space Complexity: O(1) for C, C++, and O(N) for Python. Because in C and C++ we are performing the in-place replacement of the characters, but in Python, we have used an extra string.

    Conclusion

    Now let's wrap up our article on "Write a Program to convert the first letter of each word of a string to uppercase and other to lowercase". In this article, we learned how to convert the first letter of each word into uppercase and the rest of the letters into lowercase.

    People are also reading:

    Leave a Comment on this Post

    0 Comments