Answer to Question #56473 in C++ for Nithya
Question #56473
Write a program that reads a string them changes capital letters with small letters and small letters with capital letters.
Note that if s is a string, s.lenght() returns the length of the string, s[i] is the character in the string at position i .
The first character in a string has index 0;
As an example, cout << s[2]; will display the third character in the string. s[3]=ādā; will replace the third character in the string with ādā.
Note that if s is a string, s.lenght() returns the length of the string, s[i] is the character in the string at position i .
The first character in a string has index 0;
As an example, cout << s[2]; will display the third character in the string. s[3]=ādā; will replace the third character in the string with ādā.
Expert's answer
#include <iostream>
#include <string>
#include <ctype.h>
using namespace std;
int main() {
string buffer;
getline(cin,buffer);
for(unsigned int i=0; i<buffer.length(); i++) {
if(buffer[i]>='A' && buffer[i]<='Z') buffer[i]=tolower(buffer[i]);
else if(buffer[i]>='a' && buffer[i]<='z') buffer[i]=toupper(buffer[i]);
}
cout<<buffer;
return 0;
#include <string>
#include <ctype.h>
using namespace std;
int main() {
string buffer;
getline(cin,buffer);
for(unsigned int i=0; i<buffer.length(); i++) {
if(buffer[i]>='A' && buffer[i]<='Z') buffer[i]=tolower(buffer[i]);
else if(buffer[i]>='a' && buffer[i]<='z') buffer[i]=toupper(buffer[i]);
}
cout<<buffer;
return 0;
Need a fast expert's response?
Submit orderand get a quick answer at the best price
for any assignment or question with DETAILED EXPLANATIONS!
Comments
Leave a comment