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;
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.

i had a error---> cannot convert char * to char when i write this code as below

while (tokenPtr != NULL)
{
cout<<tokenPtr<<"\n";
temp= tokenPtr;//tokenPtr was declare as a pointer
i++;
tokenPtr=strtok(NULL," ");
}

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

Edited 3 Years Ago by happygeek: fixed formatting

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

Edited 3 Years Ago by happygeek: fixed formatting

now..just need to think how to compare the token(word) with word in the file.txt that contain many word.
wish me luck

here 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;}
}

same as below,but now the code in the tags..
thank you

Write a Java Applications to extract a portion of a character string and print the extracted 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.

This article has been dead for over six months. Start a new discussion instead.