(Using C++ Codes) Exercise #3: Some operations on Strings Write a program that u
ID: 3819358 • Letter: #
Question
(Using C++ Codes)
Exercise #3: Some operations on Strings Write a program that uses a C-string (character array) to store a string entered from the keyboard. The program will then provide 4 options about operations performed on the input string in the form of a m as shown in the following sample Input/Output. The 4 operations are given below: reverse the string 2. print the length of the string convert the string c e. from upper case to lower and from lower to upper case print number of words in the string Note: It is not allowed to use the string library functions for the 4 operations. Sample input/output: MANGO IS sweet Enter a string of your choice of size 100 Cnaximum Press 1 to reverse the string Press 2 to print the length of the string Press 3 to convert the string case Clower case to upper and vice versa Press 4 print number of words in the string teews SI OGNAM 8.315 s Process returned 0 X0x0 execution time Press any key to continue Enter a String of your choice of size 100 Kmaximum MANGO IS sweet Press 1 to reverse the string Press 2 to print the length of the string Press 3 to convert the string case Clower case to upper and vice versa> Press 4 print number of words in the string ength is 15 rocess returned 0 X2x0 execution time 8.754 s ress any key to continue Enter a String of your choice o 00 (maximum MANGO IS sweet SC 12e Press 1 to reverse the string Press 2 to print the length of the string Press 3 to convert the string case Clower case to upper and vice versa Press 4 print number of words in the string mango is SWEET Process returned 0 K0x0> execution time 8.856 s Press any key to continue Enter a string of your choice of size 100 Kmaximum MANGO IS sweet Press 1 to reverse the string Press 2 to print the length of the string Press 3 to convert the string case Clower case to upper and vice versa> Press 4 print number of words in the string rocess returned 0 X0x0 execution time 8.361 s ess any key to continue.Explanation / Answer
#include <iostream>
using namespace std;
string reverse(char str[100])
{
int i,j;
char temp;
j=0;
while(str[j]!='')
{
j=j+1;
}
j=j-1;
i=0;
while(i<j)
{
temp=str[i]; //swap characters,first with the last,second with second last and so on until i<j
str[i]=str[j];
str[j]=temp;
i++;
j--;
}
return str;
}
int printLength(char string1[100])
{
char *str = string1;
int length = 0;
while(*str)
{
length++;
str++;
}
return length;
}
void ChangeCase(char str[100])
{
int i=0;
while(str[i] != '')
{
if(str[i] >='A' && str[i] <='Z')
{
str[i] = str[i] + 32;
}
else if(str[i] >='a' && str[i] <='z')
{
str[i] = str[i] - 32;
}
i++;
}
}
int words(char string1[100])
{
char *str = string1;
int words = 1;
while(*str)
{
if(*str == ' ' || *str == ' ') // if any charater is space or tab
words++;
str++;
}
return words;
}
int main()
{
char string1[100];
int option;
cout<<"Enter the string : ";
cin.getline(string1,100);
cout<<" 1. Reverse the string";
cout<<" 2. Print the length of string";
cout<<" 3. Convert the string case ie from upper case to lower and lower to upper case";
cout<<" 4. print number of words in a string";
cout<<" Enter the option";
cin>>option;
switch(option)
{
case 1: cout<<" Reverse of string : "<<reverse(string1);
break;
case 2: cout<<" Length of the string : "<<printLength(string1);
break;
case 3:ChangeCase(string1);
cout<<" "<<string1;
break;
case 4: cout<<"Number of words in string : "<<words(string1);
break;
default : cout<<"Invalid option";
break;
}
return 0;
}
Output:
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.