944,129 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 16187
  • C++ RSS
Feb 11th, 2006
0

extract token(word) from string

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
amen is offline Offline
17 posts
since Feb 2006
Feb 11th, 2006
0

Re: extract token(word) from string

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," ");
}
Reputation Points: 10
Solved Threads: 0
Newbie Poster
amen is offline Offline
17 posts
since Feb 2006
Feb 11th, 2006
0

Re: extract token(word) from string

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
amen is offline Offline
17 posts
since Feb 2006
Feb 12th, 2006
0

Re: extract token(word) from string

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
amen is offline Offline
17 posts
since Feb 2006
Feb 12th, 2006
0

Re: extract token(word) from string

now..just need to think how to compare the token(word) with word in the file.txt that contain many word.
wish me luck
Reputation Points: 10
Solved Threads: 0
Newbie Poster
amen is offline Offline
17 posts
since Feb 2006
Feb 12th, 2006
0

Re: extract token(word) from string

here the code
C++ Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
amen is offline Offline
17 posts
since Feb 2006
Jul 24th, 2009
0

Re: extract token(word) from string

Write a Java Applications to extract a portion of a character string and print the extracted string.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
acsountharraj is offline Offline
2 posts
since Jul 2009
Jul 24th, 2009
0

Re: extract token(word) from string

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
acsountharraj is offline Offline
2 posts
since Jul 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Problem Passing Sturcture Object To Queue - Linux?
Next Thread in C++ Forum Timeline: Find median using pointers





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC