1.

Write a C++ program to accept a sentence and count the number of times the letter ‘s’ occurs in it. For example if the sentence is This is my school’, the output should be 3.

Answer»

# include 

# include 

using namespace std;

int main()

{

char str[80], ch='s';

int i,n=0,pos;

cout<<"Enter a string";

gets(str);

for(i=0;str[i]!='\0';i++)

if(str[i]==ch)

{

n++;

pos=i;

}

if(n==1)

cout<<"The character s is present in the position"<<pos+1;

else if(n>1)

cout<<"The character s is present"<<n<<"times";

else

cout<<"The character s is not present in the string";

}



Discussion

No Comment Found