#include <stdio.h>
#include <string.h>
#include <conio.h>
typedef struct
{
char *token;
}token_t;
token_t *tokenlist[256];
int numtokens = 0;
char *string;
char seps[] = " ,\t\n";
char *token;
//this function gets input
char *GetInput(char *buffer)
{
int c=0, index=0;
// loop while user hasn''t hit return
while((c=getch())!=13)
{
// implement backspace
if (c==8 && index>0)
{
buffer[--index] = '' '';
printf("%c %c",8,8);
} // end if backspace
else
if (c>=32 && c<=122)
{
buffer[index++] = c;
printf("%c",c);
} // end if in printable range
} // end while
buffer[index] = 0;
if (strlen(buffer)==0)
return(NULL);
else
return(buffer);
} // end Get_Line
ProcessInput(char *buffer)
{
int count = 0;
/* Establish string and get the first token: */
token = strtok( buffer, seps );
while( token != NULL )
{
/* While there are tokens in "string" */
printf( "\n%s", token );
tokenlist[count]->token = token;
numtokens++;
/* Get next token: */
token = strtok( NULL, seps );
count++;
}
printf("\ncount = %i", count);
}
void main( void )
{
string = new char[255];
GetInput(string);
ProcessInput(string);
for(int y = 0; y < numtokens; y++);
{
printf("\n%s",tokenlist[y]->token);
}
}
thanks in advance. InFerN0
someone check my code please? It crashes when at tokenlist[count]->token = token;
InFerN0Not all who wander are lost...
Hi,
there were a few things wrong so i decided to change your code in some places. I hope you don''t mind.
Feel free to email me directly if you have other problems.
cu
Peter
there were a few things wrong so i decided to change your code in some places. I hope you don''t mind.
Feel free to email me directly if you have other problems.
#include <stdio.h>#include <stdlib.h>#include <string.h>#include <conio.h>// why not typing a normal char * here ?// typededef char * token_t; // do you want to extend that struct ????typedef struct { char * token;} token_t;#define TOKEN_MAX_STRING_LEN 256 // and this could be a char ** then ? static token_t tokenlist[TOKEN_MAX_STRING_LEN];static int numtokens = 0;static char seps[] = " ,\t\n";// I think this code does the same. void GetInput(char * buffer, size_t len){ // tell fgets to read from stdin. // this is safe because we read not more than the // provided buffer. fgets(buffer, len, stdin); }void ProcessInput(char * buffer){ char * token = NULL; token = strtok(buffer, seps); while(NULL != token) { // !!! create private copy of the token !!! // unless you do not do that your pointers // will point to the internal static buffer // of the strtok library function. tokenlist[numtokens].token = strdup(token); numtokens++; token = strtok(NULL, seps); } }void main( void ){ int y = 0; char string [TOKEN_MAX_STRING_LEN]; GetInput(string, TOKEN_MAX_STRING_LEN); ProcessInput(string); // !!! no semicolon behind the for statement // !!! or the body of the for loop is only // !!! executed once for(y = 0; y < numtokens; y++) { printf("[%d] [%s]\n", y, tokenlist[y].token); } // delete private copy of the token list for(y = 0; y < numtokens; y++) { if(NULL != tokenlist[y].token) { free(tokenlist[y].token); } } // press a key to stop getch(); }
cu
Peter
HPH
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement