C++ You are to write a program that provides for simple line editing. You progra
ID: 3908428 • Letter: C
Question
C++
You are to write a program that provides for simple line editing. You program is to do the following:
* allow the user to enter one line of text
* display a menu of user choices. The user can choose between 6 options:
1 - check if the first character in the line is capitalized, and if not, capitalize it
2 – display the first and last character in the line
3 – display the number of characters in the line
4 – check if the last character in the line is punctuation, and if not, add a period
to the end of the line
5 – display a message indicating if a word is in the line (user provides the word)
6 - replace the first occurrence of a word in the line with another word (user
provides both words)
7- print out the first word (only) in the line
Hint: you will need to use a couple of functions for this, including substr
As you can see, your program will contain one big multiway decision – performing a different action on the string depending on what the user chooses. Since there are a set number of actions (6), I would suggest allowing the user to enter the number for the action he/she desires, and using a switch statement to handle the decision making.
Be sure that your program:
* is well commented and variables are named decriptively
* displays an error message if the user selects an option that available (for example,
if options are from 1-6, and the user enters an 8)
Explanation / Answer
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
//variable declaration
string str;
int ch;
cout<<"Enter the string"<<endl;
cin>>str; //accepting the string from user
cout<<endl; //menu from user
cout<<"1.Check if the first character is Capital"<<endl;
cout<<"2.Display the first and last character"<<endl;
cout<<"3.Display the number of characters"<<endl;
cout<<"4.Check if punctuation is added at last"<<endl;
cout<<"5.Check if a word is present"<<endl;
cout<<"6.Replace the first occurrence of a word in the line with another word"<<endl;
cout<<"Enter your choice"<<endl;
cin>>ch; //accepting user choice
cout<<endl;
if(ch==1)
{
char c;
c=str[0];
if(isupper(c)) //Checkig for uppercase
cout<<c<<" It is in Uppercase"<<endl;
else
{
cout<<c<<" It is not in Uppercase"<<endl;
int k=(int)c-32;
cout<<"It is changed to Uppercase from "<<c<<" to "<<(char)k<<endl;
}
}
else if(ch==2)
{ //printing the first and the last character
int l=str.length();
cout<<"First character is "<<str[0]<<endl;
cout<<"Last character is "<<str[l-1]<<endl;
}
else if(ch==3)
{ //calcilating the totla number of chanracter
int i,alphabet=0;
for (i=0; str[i]!= ''; i++)
{
if (isalpha(str[i]))
alphabet++;
}
cout<<"Number of characters "<<alphabet<<endl;
}
else if(ch==4)
{ //checkig for punctuation
int l=str.length();
char d;
d=str[l-1];
if(d=='.')
{
cout<<"Period is already added"<<endl;
}
else
{
cout<<"Period is not there"<<endl;
cout<<"Period Added"<<endl;
cout<<str<<"."<<endl;
}
}
else if(ch==5)
{ //checkig to replace word
int i,c;
string word;
cout<<"Enter the word to find"<<endl;
cin>>word;
cout << ((-1 == str.find(word))? "Searched Word is not Found" : "Searched Found") << endl;
}
else if(ch==6)
{
string wrd;
int l;
string word1;
cout<<"Enter the word to find"<<endl;
cin>>wrd;
cout<<"Enter the word to replace"<<endl;
cin>>word1;
l=wrd.length();
while (str.find(wrd) != string::npos)
str.replace(str.find(wrd),l, word1);
cout <<"The new string is "<< str << endl;
}
else
{ cout<<"Invalid Choice"<<endl;
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.