Flushing the previous input and prompting user to enter again in same string array.
Jun 3, 2018 at 8:36am UTC
So I got homework to make a program that does the following :
1. Calculate the length of the string
2.Reverse the string
3.Replace character (user input character) in the string
4.Replace Spaces
5.The user should be able to use the menu as many times he/she wants.
6.Prompts the user to enter a new string and work the same functions upon it as many times the user wants
its the 6th which i can't quite figure out,would i need to make a new string array or something?
Please help with explanation if possible,thank you very much.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
#include<iostream>
#include<string.h>
#include<stdio.h>
using namespace std;
class stringfuncs
{
private :
char str[200];
int len = 0;
void revstr()
{
cout<<"\nThe Reversed String Is : \t" ;
int p = strlen(str);
for (int i = p - 1 ; i >= 0 ;i--)
cout<<str[i];
}
void calcstrlen()
{
cout<<"\nThe Length Of String Is = " ;
for (int x = 0 ; str[x] != '\0' ; ++x)
++len;
cout<<len;
}
void replacechar()
{
char tbrplcd,tbrplcdwith;
cout<<"\nEnter The Character You Want To Replace ?" ; cin>>tbrplcd ;
cout<<"\nEnter The Character You Want To Replace It With ? " ; cin>>tbrplcdwith;
for (int y = 0 ; str[y] != '\0' ; ++y)
{
if (str[y] == tbrplcd)
{
str[y] = tbrplcdwith;
}
else
str[y] = str[y];
}
puts(str);
}
void replacespaces()
{
char spcrplc;
cout<<"\nWith Want You Want TO Replace The Spaces ?" ;
cin>>spcrplc;
for (int z = 0 ; str[z] != '\0' ; ++z)
{
if (str[z] == ' ' )
{
str[z] = spcrplc;
}
else
str[z] = str[z];
}
puts(str);
}
public :
void readstr()
{
cout<<"\nEnter The STRING Please :" ;
gets(str);
}
void dispstr()
{
cout<<"\nThe STRING IS : " ; puts(str);
}
void stringmenu()
{
int choice;
char choice1;
do
{
cout<<"\n\n1.Want To Reverse The String\n2.Calculate The Length\n3.Replace A Character\n4.Replace Spaces" ;
cin>>choice ; if (choice == 1)
{
revstr();
}
else if (choice == 2)
{
calcstrlen();
}
else if (choice == 3)
{
replacechar();
}
else if (choice == 4)
{
replacespaces();
}
cout<<"\nUse The Menu Again? (Y/N)" ;
cin>>choice1;
if (choice1 == 'n' || choice1 == 'N' )
cout<<"\n\nThanks For Using This Program!! Made BY AKSHAT" ;
}
while (choice1 =='y' || choice1 == 'Y' )
; }
};
int main()
{
char xoice;
stringfuncs obj1,obj2;
obj1.readstr();
obj1.dispstr();
obj1.stringmenu();
return 0;
}
Last edited on Jun 3, 2018 at 8:40am UTC
Jun 3, 2018 at 6:27pm UTC
Add a loop into the main(), where lines 105-108 are in the body of the loop.
Jun 6, 2018 at 5:02pm UTC
Thanks! Was an easy tweak ! much appreciated.
Topic archived. No new replies allowed.