954,480 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

missing characters in printing string

hi,
this is something i keep running into.
for example, in the following example of strtok, i'm splitting a given string on a space(" ") delimiter:

#include<stdio.h>
#include<string.h>
int main()
{
char str[]="1234563 34 7898";
char delim[]=" ";
char* result=NULL;
char new[15][3];
int i=0;
int j=0;
bzero(new, sizeof new);
result=strtok(str,delim);
i=0;
while( result != NULL ) {
   printf( "result is \"%s\"\n", result );
      strcpy(new[i],result);
i++;
printf("new[%d]=%s\n",i,new[i]);
    result = strtok( NULL, delim );
}
return 0;
}


while result prints the output correctly, the contents of new[] are printed truncated.
happens a lot of times and is probably a very important concept i'm missing out on. whats the way out?
Thanks!

xenanovich
Newbie Poster
10 posts since May 2010
Reputation Points: 10
Solved Threads: 0
 

You're incrementing i in the wrong place.

aspire1
Light Poster
43 posts since Apr 2010
Reputation Points: 46
Solved Threads: 14
 
char new[15][3];

Make it:

char new[3][15];


[15][3] Means 15 rows of 3 cols
[3][15] Means 3 rows of 15 cols

nbaztec
Posting Pro in Training
475 posts since May 2010
Reputation Points: 57
Solved Threads: 60
 

Thank you aspire and nisheeth!

xenanovich
Newbie Poster
10 posts since May 2010
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: