Answer to Question #49711 in C++ for nada
Question #49711
Write C++ program that reads a string that has series of characters (maximum 10 characters), constituting therefore a meaningless string (i.e., the string does not have a meaning). Afterwards, the program asks the user several times the two characters he would like them to be swapped. To this purpose, the user has to specify the positions of these two characters. When the program stops, the user could end-up producing a meaningful string.
Expert's answer
#include<iostream.h>
#include<string.h>
#include<conio.h>
#define MAX 1000
int main()
{
char *str=new char[MAX];
int i,f,s;
cout<<"Input the string:";
cin.getline(str,MAX);
char ans='Y',temp;
while((ans=='Y')||(ans=='y'))
{
cout<<"first position:";
cin>>f;
cout<<"second position:";
cin>>s;
if((f<strlen(str))&&(s<strlen(str)))
{
temp=str[s];
str[s]=str[f];
str[f]=temp;
}
cout<<"You are want changeone more?";
cin>>ans;
}
cout<<"Result:"<<str;
getch();
return 0;
}
#include<string.h>
#include<conio.h>
#define MAX 1000
int main()
{
char *str=new char[MAX];
int i,f,s;
cout<<"Input the string:";
cin.getline(str,MAX);
char ans='Y',temp;
while((ans=='Y')||(ans=='y'))
{
cout<<"first position:";
cin>>f;
cout<<"second position:";
cin>>s;
if((f<strlen(str))&&(s<strlen(str)))
{
temp=str[s];
str[s]=str[f];
str[f]=temp;
}
cout<<"You are want changeone more?";
cin>>ans;
}
cout<<"Result:"<<str;
getch();
return 0;
}
Need a fast expert's response?
Submit orderand get a quick answer at the best price
for any assignment or question with DETAILED EXPLANATIONS!
Comments
Leave a comment