Convert a C++ String to Upper Case

C++ProgrammingServer Side Programming

Here is the program to convert a string to uppercase in C++ language,

Example

 Live Demo

#include<iostream>
#include<string.h>
using namespace std;

int main() {
   char s[30] = "This_is_string";
   int i;

   for(i=0;i<=strlen(s);i++) {
      if(s[i]>=97 && s[i]<=122) {
         s[i]=s[i]-32;
      }
   }
   cout<<"The String in Uppercase = "<<s;
   return 0;
}

Output

Here is the output

The String in Uppercase = THIS_IS_STRING

In the program, the actual code of conversion of string to upper case is present in main() function. An array of char type s[30] is declared which stores the given string.

Then, for loop is used to convert the string into upper case string and if block is used to check that if characters are in lower case then, convert them in upper case by subtracting 32 from their ASCII value.

for(i=0;i<=strlen(s);i++) {
   if(s[i]>=97 && s[i]<=122) {
      s[i]=s[i]-32;
   }
}
raja
Published on 23-Oct-2018 12:20:48
Advertisements