Click to See Complete Forum and Search --> : Simple Program
diana_j86
03-19-2007, 06:14 PM
Hello All,
Well, I am a beginner in C++, and I am trying to print each word of a string on a separate line, here is my code:
void main(){
char a[100]="hello, how are you doing?";
char *p=a;
while(*p){
if(*p!=',' && *p!=' ' && *p!='?'){
cout<<*p;
p++;
}
else{
cout<<endl;
p++;
}
}//while
}//main
The problem in this code is that after the word "hello" it leaves another line, so the output looks like this
hello
how
are
you
diong
I don't understand what I can do so I let each word be on a separate line without leaving empty lines...
Thanks,
Ivan**
03-20-2007, 04:12 AM
Hello All,
Well, I am a beginner in C++, and I am trying to print each word of a string on a separate line, here is my code:
void main(){
char a[100]="hello, how are you doing?";
char *p=a;
while(*p){
if(*p!=',' && *p!=' ' && *p!='?'){
cout<<*p;
p++;
}
else{
cout<<endl;
p++;
}
}//while
}//main
The problem in this code is that after the word "hello" it leaves another line, so the output looks like this
hello
how
are
you
diong
I don't understand what I can do so I let each word be on a separate line without leaving empty lines...
Thanks,
This may not be the most efficient way to do this but I guess it'll work
else{
cout<<endl;
while (*p ==',' || *p ==' ' || *p =='?') //advance to the next real character
p++;
}
diana_j86
03-20-2007, 11:27 AM
Thanks Ivan, it worked, well, how to make it more iffecient?...will it be better if I use strtok(s," "), but I don't understand how it works?
Thanks
diana_j86
03-20-2007, 11:38 AM
int main ()
{
char str[] ="- This, a sample string.";
char * pch;
cout<<str<<endl;
pch = strtok (str," ,.-");
while (pch != NULL)
{
cout<<pch<<endl;
pch = strtok (NULL, " ,.-");
}
return 0;
}
i found how to use strtok(str," "), do u think it's more efficient?...or it can be solved in a better way?
Thanks,
drkybelk
03-20-2007, 12:19 PM
Hi,
for a beginners program I guess this is efficient enough. You're only trying to learn how to handle pointers/advance them/de-reference them ...
If you get a bit more serious about programming you should look for ways of how to avoid pointers where you don't need them. Pointers are a major source of grieve, esp. when you're new to the language and it takes a while to get a full grip of them. There are however very good alternatives for most cases and I'd recommend you have a look into them early in your career.
For your problem at hand: rather than using char* and buffers, try the STL. Particularily have a look at std::string and its methods find and substr.
Cheers,
D
drkybelk
03-20-2007, 12:22 PM
ADDENDUM:
Please don't get me wrong: You need to know how pointers work, definitely! So sometime in your endeavours you will have to face up to them. ;-)
Don't mistake this with having to use them at every possible occasion, though.
diana_j86
03-20-2007, 01:35 PM
Thanks drkybelk,
Can I ask this?....in which cases it is necessary to delete the pointer??
Thanks again,
drkybelk
03-20-2007, 01:53 PM
It's neccessary when you allocated memory with the new operator like this:
char* pC1 = new char;
char* pC2 = new char[42];
delete [] pC2;
delete pC1;
But then there is one reason why I don't like pointers:
who is responsible for the deletion?
consider:
void someFunc(int sz, char** pLocal)
{
// here I might actually delete pGlobal
delete [] *pLocal;
}
char * pGlobal = new char [101];
someFunc(101,&pGlobal);
// pGlobal might be deleted here
delete [] pGlobal; // attempt to delete again. run-time-error
With references on the other hand it is always clear who is responsible for the deletion of the referred to object.
Cheers,
D
Danny
03-21-2007, 05:11 AM
you need to delete any pointer that has been allocated using new or new[].
Ivan's solution is actually more efficient than strtok but I would replace the three ORed conditions with a simple isalpha() call. The isalpha() macro is defined in <cctype>.
devx.com
Copyright Internet.com Inc. All Rights Reserved