Hi,
Before posting anything here I searched google and the forum but unfortunately could find a solution.
The problem is, user enters max. 80 characters, program reads the input and reverse the sentence.
Example: Input is "This is just an example"
Output should be "example an just is this"
Here is the code,
#include<iostream>
#include<string>
#include<cstring>
using namespace std;
int main()
{
char mystring[80];
char *p;
int count=0, i,j=0;
cout<<"Enter a sentence up to 80 characters \n\n";
cin.getline (mystring,80);
for (i=0;i<80;i++)
{
if (mystring==' ')
count=count+1;
}
int *array = new int [count];
cout << "The string is: [" << mystring << "]\n";
p = strtok(mystring, " ");
*(array+j)=*p;
while ( p ) {
cout << "\t" << p << "\n";
p = strtok(NULL, " ");
j++;
*(array+j)=*p;
}
cout<<"array+j= "<<(array+j)<<endl;
delete [] array;
return 0;
}
Other than this code, I tried some different things but none gave me what I needed. This code even gives an error on windows XP.
Appreciate for any help.
Before posting anything here I searched google and the forum but unfortunately could find a solution.
The problem is, user enters max. 80 characters, program reads the input and reverse the sentence.
Example: Input is "This is just an example"
Output should be "example an just is this"
Here is the code,
#include<iostream>
#include<string>
#include<cstring>
using namespace std;
int main()
{
char mystring[80];
char *p;
int count=0, i,j=0;
cout<<"Enter a sentence up to 80 characters \n\n";
cin.getline (mystring,80);
for (i=0;i<80;i++)
{
if (mystring==' ')
count=count+1;
}
int *array = new int [count];
cout << "The string is: [" << mystring << "]\n";
p = strtok(mystring, " ");
*(array+j)=*p;
while ( p ) {
cout << "\t" << p << "\n";
p = strtok(NULL, " ");
j++;
*(array+j)=*p;
}
cout<<"array+j= "<<(array+j)<<endl;
delete [] array;
return 0;
}
Other than this code, I tried some different things but none gave me what I needed. This code even gives an error on windows XP.
Appreciate for any help.