extract token(word) from string

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Feb 2006
Posts: 17
Reputation: amen is an unknown quantity at this point 
Solved Threads: 0
amen amen is offline Offline
Newbie Poster

extract token(word) from string

 
0
  #1
Feb 11th, 2006
i need help.
i need to write a code that can extract word from string.
i had use cin.getline(string,size) to get th string and strtok(string," ") to extarct token. now i want to know how can i insert the token in array because i want to use the token for other purpose.

this what i had write

const size=100;
char string[size];
char *tokenPtr;
cout<<"string\n";
cin.getline(string,size);
tokenPtr =strtok(string," "); //bring string1 by separated by characters in string2

while (tokenPtr != NULL)
{
cout<<tokenPtr<<"\n";
tokenPtr=strtok(NULL," ");
}

i had come across function strcspn, and i want to know how it work.

please give me some idea?
thank your.
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 17
Reputation: amen is an unknown quantity at this point 
Solved Threads: 0
amen amen is offline Offline
Newbie Poster

Re: extract token(word) from string

 
0
  #2
Feb 11th, 2006
i had a error---> cannot convert char * to char when i write this code as below

while (tokenPtr != NULL)
{
cout<<tokenPtr<<"\n";
temp[i]= tokenPtr;//tokenPtr was declare as a pointer
i++;
tokenPtr=strtok(NULL," ");
}
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 17
Reputation: amen is an unknown quantity at this point 
Solved Threads: 0
amen amen is offline Offline
Newbie Poster

Re: extract token(word) from string

 
0
  #3
Feb 11th, 2006
i had change to code to this
while (tokenPtr != NULL)
{

temp[i]=* tokenPtr;
i++;
tokenPtr=strtok(NULL," ");
}

for (int j=0;j<i;j++)
{cout <<temp[j]<<"\n";
}

the input was i need help
but only show first character of the word as
i
n
h

how can i get the output like this
i
need
help
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 17
Reputation: amen is an unknown quantity at this point 
Solved Threads: 0
amen amen is offline Offline
Newbie Poster

Re: extract token(word) from string

 
0
  #4
Feb 12th, 2006
i think i had got it.it works

here is the code

#include <iostream.h>
#include <string.h>

int main()
{
//declaraction variable
//----------------------------
const size=100; //size of string
char string[size],temp[size][32];
char *tokenPtr;
int i=0;

cout<<"input string\n";
cin.getline(string,size);//read line of string

/*function char *strtok(char *s1,const char *s2) breaks s1 into token(word)
separated by character in s2
*/

tokenPtr =strtok(string," ");//pointer to first token

while (tokenPtr != NULL)
{
strcpy(temp[i],tokenPtr);//save the token to array
i++;
tokenPtr=strtok(NULL," ");
}

for(int j=0;j<i;j++)
{
cout<<"temp["<<j<<"]="<<temp[j]<<"\n"; //write the token
}
cout<<"\n\n0 for exit\n";
int b;
cin>>b;
if (b==0)
{return 0;}
}

i try to input string like--> here i go
then output show as below

temp[0]=here
temp[1]=i
temp[3]=go

let me know if there a simple or other way to get the program run..

thank you
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 17
Reputation: amen is an unknown quantity at this point 
Solved Threads: 0
amen amen is offline Offline
Newbie Poster

Re: extract token(word) from string

 
0
  #5
Feb 12th, 2006
now..just need to think how to compare the token(word) with word in the file.txt that contain many word.
wish me luck
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 17
Reputation: amen is an unknown quantity at this point 
Solved Threads: 0
amen amen is offline Offline
Newbie Poster

Re: extract token(word) from string

 
0
  #6
Feb 12th, 2006
here the code
  1. #include <iostream.h>
  2. #include <string.h>
  3.  
  4. int main()
  5. {
  6. //declaraction variable
  7. //----------------------------
  8. const size=100; //size of string
  9. char string[size],temp[size][32];
  10. char *tokenPtr;
  11. int i=0;
  12.  
  13. cout<<"input string\n";
  14. cin.getline(string,size);//read line of string
  15.  
  16. /*function char *strtok(char *s1,const char *s2) breaks s1 into token(word)
  17.   separated by character in s2
  18. */
  19.  
  20. tokenPtr =strtok(string," ");//pointer to first token
  21.  
  22. while (tokenPtr != NULL)
  23. {
  24. strcpy(temp[i],tokenPtr);//save the token to array
  25. i++;
  26. tokenPtr=strtok(NULL," ");
  27. }
  28.  
  29. for(int j=0;j<i;j++)
  30. {
  31. cout<<"temp["<<j<<"]="<<temp[j]<<"\n"; //write the token
  32. }
  33. cout<<"\n\n0 for exit\n";
  34. int b;
  35. cin>>b;
  36. if (b==0)
  37. {return 0;}
  38. }

same as below,but now the code in the tags..
thank you
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 2
Reputation: acsountharraj is an unknown quantity at this point 
Solved Threads: 0
acsountharraj acsountharraj is offline Offline
Newbie Poster

Re: extract token(word) from string

 
0
  #7
Jul 24th, 2009
Write a Java Applications to extract a portion of a character string and print the extracted string.
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 2
Reputation: acsountharraj is an unknown quantity at this point 
Solved Threads: 0
acsountharraj acsountharraj is offline Offline
Newbie Poster

Re: extract token(word) from string

 
0
  #8
Jul 24th, 2009
1. Write a Java Applications to extract a portion of a character string and print the extracted string.
2. Write a Java Program to implement the concept of multiple inheritance using Interfaces.
3. Write a Java Program to create an Exception called payout-of-bounds and throw the exception.
4. Write a Java Program to implement the concept of multithreading with the use of any three
multiplication tables and assign three different priorities to them.
5. Write a Java Program to draw several shapes in the created windows.
6. Write a Java Program to create a frame with four text fields as name, street, city and pin code
with suitable tables. Also add a button called “my details”, When the button is clicked its
corresponding values are to be appeared in the text fields.
7. Write a Java Program to demonstrate the Multiple Selection List-box.
8. Write a Java Program to create a frame with three text fields for name, age and qualification and
a text field for multiple lines for address.
9. Write a Java Program to create Menu Bars and pull down menus.
10. Write a Java Program to create frames which respond to the mouse clicks. For each events with
mouse such as mouse up, mouse down, etc., the corresponding message to be displayed.
11. Write a Java Program to draw circle, square, ellipse and rectangle at the mouse click positions.
12. Write a Java Program which open an existing file and append text to that file.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 10505 | Replies: 7
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC