Hi,
I am learning C++ and I have the following code:
//Tokenizing program: pointer version.
#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
char str[80];
char token[80];
char *p, *q;
cout << "Enter a sentence: ";
gets(str);
p=str;
//read a token at a time from a string
while (*p)
{
q=token;
/*read a character until either a space or a null character is encounterd*/
while (*p && *p!= ' ')
{
*q=*p;
q++;
p++;
}
if (*p)
p++;
*q = '\0';
cout << token << endl;
}
return 0;
}
When I changed the second while statement to an if statement, as:
//Tokenizing program: pointer version.
#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
char str[80];
char token[80];
char *p, *q;
cout << "Enter a sentence: ";
gets(str);
p=str;
//read a token at a time from a string
while (*p)
{
q=token;
/*read a character until either a space or a null character is encounterd*/
if (*p && *p!= ' ')
{
*q=*p;
q++;
p++;
}
else if (*p)
p++;
*q = '\0';
cout << token << endl;
}
return 0;
}
I started to have the output written in a vertical form, that is one letter at line.
What is the difference between the two codes?
Thanks,
faizlo