Program to Convert first character uppercase in a String
Code
#include <bits/stdc++.h>
using namespace std;
char lower(char c){ // to check & convert uppercase to lowercase
if(c>=65 && c<=90){
return c+32;
}
else{
return c;
}
}
char upper(char c){ //to check & convert lowercase to uppercase
if(c>=97 && c<=122){
return c-32;
}
else{
return c;
}
}
int main()
{
string s; //requesting a string
getline(cin,s);
int l=s.length(); //checking length
for(int i=0;i<l;i++){ //loop to go through each char of string
if(i==0){ //0th char is converted to uppercase
s[i]=upper(s[i]);
}
else if(s[i]==' '){ //after each space the char is converted to uppercase
s[i+1]=upper(s[i+1]);
i++;
}
else{ //rest all the characters are converted to lowercase
s[i]=lower(s[i]);
}
}
cout<<"The String is: "<<s<<endl; //printing the string
}
Input:
coding dEMONS
Output:
The String is: Coding Demons
0 Comments