Write a C++ program that asks the user to enter a string value in a variable named “Str”. The input string should be of size 20 characters (minimum). Next, apply following operation (in sequence) on the input string: 1) Find and print the length of the string. 2) Display string in reverse order. 3) Concatenate “Hello World” to the string. 4) Count number of words in updated string. 5) Convert whole string in lowercase.
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
char str[1024];
cout<<"Enter string value: ";
cin.getline(str,sizeof(str));
int strLen = strlen(str);
if(strLen<20){
cout<<"Incorrect data"<<endl;
return 1;
}
cout<<"Lenght of string "<<strLen<<endl;
cout<<"String in reverse order: ";
for(int i=strLen-1;i>=0;i--)
cout<<str[i];
cout<<endl;
strcat(str,"Hello World");
int countOfWords=0;
int newStrLen = strlen(str);
for(int i=1;i<newStrLen;i++)
if(str[i-1]!=' '&&str[i]==' ')
countOfWords++;
cout<<"Number of words in updated string: "<<countOfWords<<endl;
for(int i=0;i<newStrLen;i++)
str[i]=tolower(str[i]);
cout<<str<<endl;
return 0;
}
Comments
Leave a comment