C++ Program to Capitalize Given Character in String

This article provides a program in C++ that capitalizes only specific/particular character from a string. Here the string and the character must be entered by user at run-time of the program.

For example, if user enters Hey!, this is codescracker as string and e as character, then the output will be hEy!, this is codEscrackEr. That is, all e (lowercase) gets converted into E (uppercase) in given string.

Capitalize Specific Character in String

The question is, write a program in C++ that receives a string and a character as input and capitalizes all the given character from the given string. The program given below is the answer to this question:


using namespace std;
int main()
   char str[200], ch, ch_temp;
   int len, i, asc_val;
   cout<<"Enter the String: ";
   cout<<"Enter the Character to Capitalize: ";
   len = strlen(str);
   for(i=0; i<len; i++)
         asc_val = str[i];
         if(asc_val>=97 && asc_val<=122)
            asc_val = asc_val-32;
            ch_temp = asc_val;
            str[i] = ch_temp;
   cout<<"\nAll \""<<ch<<"\" gets capitalized";
   cout<<"\nThe new string is\n\n";
   return 0;

The snapshot given below shows the initial output produced by above C++ program, that is used to capitalize all given character from given string:

c++ capitalize given character from string

Now supply the inputs say Hey!, this is codescracker as string and e as character to capitalize. Here is the sample run with exactly same user input as given here:

capitalize given characters from string c++

Here, I've used the ASCII value to capitalize the character. That is the ASCII values of a-z are 97-122 whereas the ASCII values of A-Z are 65-90 Therefore subtracting 32 means converting lowercase to uppercase.

C++ Online Test

« Previous Program Next Program »

Follow/Like Us on Facebook

Subscribe Us on YouTube