Loading....
Coupon Accepted Successfully!

 

More Programs

18.26 Write a program to count total number of vowels present in the given string.

#include<iostream>

#include<string>

using namespace std;

int main()

{

string s1;

string s2(“aeiou”);

int i,j,c=0;

cout<<“\n Enter a string:”;

getline(cin,s1);

int l=s1.length();

for (i=0;i<l;i++)

{

for (j=0;j<5;j++)

if ( s1.at(i)==s2.at(j)) c++;

}

cout<<“\n Total vowels present are:”<<c;

return 0;

}

OUTPUT

Enter a string : c plus plus

Total vowels present are : 2

Explanation: In the above program, the string objects s1 and s2 are declared. The s2 is initialized with the string “aeiou”. The string entered through the keyboard is assigned to the string object s1. Using the nested for loop and the if condition, vowels are checked in the main string. If a vowel is found, the counter variable c is incremented. At last, the value of c gives us the total number of vowels present in the string. The function at()gives the character at a particular ­location.

18.27 Write a program to find the length of the string excluding spaces.

#include<iostream>

#include<string>

using namespace std;

int main()

{

string s1;

cout<<“\n Enter a string:”;

getline(cin,s1);

int v=s1.length(),s=0;

for (int m=0;m<v;m++)

{

if (s1.at(m)==‘ ’)

s++;

}

cout<<“\n Length of the string excluding spaces:”<<v-s;

return 0;

}

OUTPUT

Enter a string : C PLUS PLUS

Length of the string excluding spaces : 9

Explanation: In the above program, s1 is a string object. The string entered through the keyboard is assigned to the object s1. The length of the string s1 is calculated using the length() function and stored in the variable v. The for loop executes from 0 to v. The if condition checks every character of the string to see whether it a blank space or not. If it is a blank space, the counter variable s is incremented. When the for loop terminates, the variable s contains the total number of blank spaces present in the string. The string length is calculated by subtracting the spaces from the original length, that is, l-s.

18.28 Write a program that will read a line of text. Replace all the blank spaces with an underscore ( _).

#include<iostream>

#include<string>

using namespace std;

int main()

{

string s1;

cout<<“\n Enter a string:”;

getline(cin,s1);

int l=s1.length();

for (int m=0;m<l;m++)

{

if (s1.at(m)==‘ ’)

s1.at(m)=‘_’;

}

cout<<“\n s1:”<<s1;

return 0;

}

OUTPUT

Enter a string : C PLUS PLUS

s1 : C_PLUS_PLUS

Explanation: The above program is similar to the previous one. Here, when a blank space is found, it is replaced with the sign “_” (underscore). The statement s1.at (m)=‘_’ performs this task.

18.29 Write a program to display the reverse string of the entered string.

#include<iostream>

#include<string>

using namespace std;

int main()

{

string s1;

cout<<“\n Enter a string:”;

getline(cin,s1);

int v=s1.length();

for (int m=v;m>=0;m--)

cout<<s1[m];

return 0;

}

OUTPUT

Enter a string : WORLD

DLROW

Explanation: In the above program, the length of the string object is stored in the variable v. The for loop executes from v to 0, that is, in descending order. The statement cout<<s1[m] displays the characters on the screen.





Test Your Skills Now!
Take a Quiz now
Reviewer Name