In c++ char x[10]; x[0] = \'a\'; x[1] = \'b\'; x[2] = \'c\'; x[3] = \'d\'; x[4]
ID: 3836773 • Letter: I
Question
In c++
char x[10]; x[0] = 'a'; x[1] = 'b'; x[2] = 'c'; x[3] = 'd'; x[4] = 'e'; x[5] = 'f'; x[6] = 'g'; x[7] = 'h'; x[8] = 'i'; x[9] = 'j';
for (int i = 0; i<10; i++){ cout<< x[i]; // print each character cout << " "; }
cout << endl
; for (int i = 0; i<10; i++
) { cout << (int)x[i]; // The actual data in x[0] is 97, the ascii code of ‘a’. cout << " "; }
cout<<(char)(x[0]-32); //’a’ is printed as ‘A’
in this code using Ascii Table Get a string as an array ,
when lowercase, change upper case.
or when upper case change lower case.
When change these letter have to using Ascii Table
ex) Hellow => hELLOW
Explanation / Answer
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str;
int size,i;
cout<<"Enter the string : ";
cin>>str;
size = str.length();
for(i=0;i<size;i++)
{
if((int)str[i]<97) //To check char is Upper case
{
str[i]=(char)((int)str[i]+32); //convert Upper case to Lower case
}
else if((int)str[i]>97) //To check char is Lower case
{
str[i]=(char)((int)str[i]-32); //convert lower case to upper case
}
}
cout<<"Output String is "<<str;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.