D
DFS
Using strcat in the middle of looping thru the tokens actually changes
the tokens occurring after the point at which strcat is called.
Anybody know why? And how to fix it?
Thanks
====================================================================================
Output:
[dfs@home files]$ ./strtokens
* using strtok, with no calls to strcat
string before: Look at him working, darning his socks in the night when
there's nooboody there
token: Look
token: at
token: him
token: working,
token: darning
token: his
token: socks
token: in
token: the
token: night
token: when
token: there's
token: nooboody
token: there
string after: Look
Longest word is 8 characters
-----------------------------------------------------------------------------
* using strtok, with calls to strcat
string before: Look at him working, darning his socks in the night when
there's nooboody there
token: Look
token: at
token: him
token: working,
(this token has strlen(8), so strcat is called
and it alters the remaining tokens, including
those in the next call to strtok_r?!?)
token: da
token: h
token: s
token: s
token: c
token: s
token: the
token: ht
token: whe
token: the
token: e's
token: b
token: dy
token: the
token: e
string after: Look
Longest word(s), len 8: working,
-----------------------------------------------------------------------------
* using strtok_r, with calls to strcat
string before: Look at him working, darning his socks in the night when
there's nooboody there
token: L
token: at
token: h
token: m
token: w
token: da
token: h
token: s
token: s
token: c
token: s
token: the
token: ht
token: whe
token: the
token: e's
token: b
token: dy
token: the
token: e
string after: L
Longest word(s), len 8:
-----------------------------------------------------------------------------
Should be 'Longest word(s), len 8: working, nooboody'
====================================================================================
#include <stdio.h>
#include <string.h>
//GET LENGTH OF LONGEST WORD IN STRING
//strcat NOT USED
int getMaxWordLength(char *str, char *delim) {
char *token;
token = strtok(str, delim);
int maxWordLen = (int)strlen(token);
while(token != NULL)
{
printf("token: %s\n", token);
if ((int)strlen(token) > maxWordLen) {
maxWordLen = (int)strlen(token);
}
token = strtok(NULL, delim);
}
return maxWordLen;
}
//GET LIST OF LONGEST WORDS IN STRING.
//USE strtok AND strcat
int getLongestWords(char *str, char *longestWords, int maxWordLen, char
*delim) {
char *token;
token = strtok(str, delim);
while(token != NULL)
{
printf("token: %s\n", token);
if ((int)strlen(token) == maxWordLen) {
strcat(longestWords, token);
strcat(longestWords, " ");
}
token = strtok(NULL, delim);
}
return 0;
}
//GET LIST OF LONGEST WORDS IN STRING.
//USE strtok_r AND strcat
int getLongestWords_r(char *str, char *longestWords, int maxWordLen,
char *delim) {
char *token;
char* sptr;
token = strtok_r(str, delim, &sptr);
while(token != NULL)
{
printf("token: %s\n", token);
if ((int)strlen(token) == maxWordLen) {
strcat(longestWords, token);
strcat(longestWords, " ");
}
token = strtok_r(NULL, delim, &sptr );
}
return 0;
}
int main(void)
{
char splitStr[200] = "Look at him working, darning his socks in the
night when there's nooboody there";
char splitStrA[200] = "";
char splitStrB[200] = "";
char splitStrC[200] = "";
char delim[2] = " ";
char longestWordsOne[] = "";
char longestWordsTwo[] = "";
//MAKE COPIES OF STRING FOR USE WITH CALLS TO strtok AND strtok_r
strcpy(splitStrA, splitStr);
strcpy(splitStrB, splitStr);
strcpy(splitStrC, splitStr);
printf("string before: %s\n", splitStrA);
int i = getMaxWordLength(splitStrA, delim);
printf("string after: %s\n", splitStrA);
printf("Longest word is %d characters\n", i);
printf("-----------------------\n");
printf("string before: %s\n", splitStrB);
getLongestWords(splitStrB, longestWordsOne, i, delim);
printf("string after: %s\n", splitStrB);
printf("Longest word(s), len %d: %s\n", i, longestWordsOne);
printf("-----------------------\n");
printf("string before: %s\n", splitStrC);
getLongestWords_r(splitStrC, longestWordsTwo, i, delim);
printf("string after: %s\n", splitStrC);
printf("Longest word(s), len %d: %s\n", i, longestWordsTwo);
printf("-----------------------\n");
return 0;
}
the tokens occurring after the point at which strcat is called.
Anybody know why? And how to fix it?
Thanks
====================================================================================
Output:
[dfs@home files]$ ./strtokens
* using strtok, with no calls to strcat
string before: Look at him working, darning his socks in the night when
there's nooboody there
token: Look
token: at
token: him
token: working,
token: darning
token: his
token: socks
token: in
token: the
token: night
token: when
token: there's
token: nooboody
token: there
string after: Look
Longest word is 8 characters
-----------------------------------------------------------------------------
* using strtok, with calls to strcat
string before: Look at him working, darning his socks in the night when
there's nooboody there
token: Look
token: at
token: him
token: working,
(this token has strlen(8), so strcat is called
and it alters the remaining tokens, including
those in the next call to strtok_r?!?)
token: da
token: h
token: s
token: s
token: c
token: s
token: the
token: ht
token: whe
token: the
token: e's
token: b
token: dy
token: the
token: e
string after: Look
Longest word(s), len 8: working,
-----------------------------------------------------------------------------
* using strtok_r, with calls to strcat
string before: Look at him working, darning his socks in the night when
there's nooboody there
token: L
token: at
token: h
token: m
token: w
token: da
token: h
token: s
token: s
token: c
token: s
token: the
token: ht
token: whe
token: the
token: e's
token: b
token: dy
token: the
token: e
string after: L
Longest word(s), len 8:
-----------------------------------------------------------------------------
Should be 'Longest word(s), len 8: working, nooboody'
====================================================================================
#include <stdio.h>
#include <string.h>
//GET LENGTH OF LONGEST WORD IN STRING
//strcat NOT USED
int getMaxWordLength(char *str, char *delim) {
char *token;
token = strtok(str, delim);
int maxWordLen = (int)strlen(token);
while(token != NULL)
{
printf("token: %s\n", token);
if ((int)strlen(token) > maxWordLen) {
maxWordLen = (int)strlen(token);
}
token = strtok(NULL, delim);
}
return maxWordLen;
}
//GET LIST OF LONGEST WORDS IN STRING.
//USE strtok AND strcat
int getLongestWords(char *str, char *longestWords, int maxWordLen, char
*delim) {
char *token;
token = strtok(str, delim);
while(token != NULL)
{
printf("token: %s\n", token);
if ((int)strlen(token) == maxWordLen) {
strcat(longestWords, token);
strcat(longestWords, " ");
}
token = strtok(NULL, delim);
}
return 0;
}
//GET LIST OF LONGEST WORDS IN STRING.
//USE strtok_r AND strcat
int getLongestWords_r(char *str, char *longestWords, int maxWordLen,
char *delim) {
char *token;
char* sptr;
token = strtok_r(str, delim, &sptr);
while(token != NULL)
{
printf("token: %s\n", token);
if ((int)strlen(token) == maxWordLen) {
strcat(longestWords, token);
strcat(longestWords, " ");
}
token = strtok_r(NULL, delim, &sptr );
}
return 0;
}
int main(void)
{
char splitStr[200] = "Look at him working, darning his socks in the
night when there's nooboody there";
char splitStrA[200] = "";
char splitStrB[200] = "";
char splitStrC[200] = "";
char delim[2] = " ";
char longestWordsOne[] = "";
char longestWordsTwo[] = "";
//MAKE COPIES OF STRING FOR USE WITH CALLS TO strtok AND strtok_r
strcpy(splitStrA, splitStr);
strcpy(splitStrB, splitStr);
strcpy(splitStrC, splitStr);
printf("string before: %s\n", splitStrA);
int i = getMaxWordLength(splitStrA, delim);
printf("string after: %s\n", splitStrA);
printf("Longest word is %d characters\n", i);
printf("-----------------------\n");
printf("string before: %s\n", splitStrB);
getLongestWords(splitStrB, longestWordsOne, i, delim);
printf("string after: %s\n", splitStrB);
printf("Longest word(s), len %d: %s\n", i, longestWordsOne);
printf("-----------------------\n");
printf("string before: %s\n", splitStrC);
getLongestWords_r(splitStrC, longestWordsTwo, i, delim);
printf("string after: %s\n", splitStrC);
printf("Longest word(s), len %d: %s\n", i, longestWordsTwo);
printf("-----------------------\n");
return 0;
}