You are on page 1of 4

String

String literals – any sequence of character enclosed in quotation marks.

Example : “Hello”

By convention, first character in string is always designated as position zero.

Strings can be also be entered from the keyboard and displayed on the screen:

#include <iostream>
#include <string>

using namespace std;

int main()
{
string message;
cout<<"Input any string value : ";
getline(cin, message);

cout<<"The string you entered is : "<<message<<endl;

system("pause");
return 0;
}
1.) Create a C++ program that will input string and display the string in reverse order.

#include <iostream>
#include <string> //length or size

using namespace std;

int main()
{
string stringinput;
int ctr, ctreachletter;

cout<<"Input string : ";


getline(cin, stringinput);

cout<<"the string entered is "<<stringinput<<endl;

cout<<"the string entered in reverse order is ";


for(ctreachletter = stringinput.length()-1; ctreachletter>=0; ctreachletter--)
{ cout<<stringinput [ctreachletter];
}
cout<<endl<<endl;

// system("pause");
return 0;
}
2.) Create a C++ program that will input a string and count how many are alphabetic
character, numeric character, and special character in that string;

#include <iostream>
#include <cctype>
#include <string> //length or size

using namespace std;

int main()
{
string stringinput;
int ctreachchar, ctralpha, ctrnumeric, ctrspecial;

cout<<"Input string : ";


getline(cin, stringinput);

cout<<"the string entered is "<<stringinput<<endl;

ctralpha=0;
ctrnumeric = 0;
ctrspecial = 0;
for(ctreachchar = 0; ctreachchar < stringinput.length(); ctreachchar++)
{ stringinput[ctreachchar]= toupper(stringinput[ctreachchar]);
if(stringinput[ctreachchar]>='A' && stringinput[ctreachchar]<='Z')
ctralpha++;
else if(stringinput[ctreachchar]>='0' && stringinput[ctreachchar]<='9')
ctrnumeric++;
else
ctrspecial++;
}
cout<<endl<<"In the string entered the number of alphabetic character is "<<ctralpha<<endl;
cout<<endl<<"In the string entered the number of numeric character is "<<ctrnumeric<<endl;
cout<<endl<<"In the string entered the number of special character is "<<ctrspecial<<endl;
// system("pause");
return 0;
}
3.) Create a C++ program that will input five strings and display the five strings in reverse
order.

#include <iostream>
#include <string> //length or size

using namespace std;

int main()
{
string stringinput [5];
int ctr, ctreachletter;

cout<<"Input any string value : "<<endl;


for(ctr=0; ctr<5; ctr++)
{ cout<<"Input value string["<<ctr<<"] = ";
getline(cin, stringinput [ctr]);
}

cout<<"the string entered are : "<<endl;


for(ctr=0; ctr<5; ctr++)
{ cout<<"string["<<ctr<<"] = "<<stringinput [ctr]<<endl;
}

cout<<"the string entered in reverse order are : "<<endl;


for(ctr=0; ctr<5; ctr++)
{ cout<<"string["<<ctr<<"] = ";
for(ctreachletter = stringinput [ctr].length()-1; ctreachletter>=0; ctreachletter--)
{ cout<<stringinput [ctr][ctreachletter];
}
cout<<endl;
}

// system("pause");
return 0;
}

You might also like