Suppose I have the following string: 0:1,2,3.
I want to separate first using : as the delimiter and when it gets to the second part (i.e 1,2,3) and try to use strtok on that (with ,) it does not work as expected.
#include <stdio.h>
#include <stdbool.h>
int main(void){
char s[10];
strcpy(s, "0:1,2,3");
char* token1 = strtok(s, ":");
//To let me know it is on the second part
bool isSecondToken = false;
while (token1) {
printf("token1: %s\n", token1);
if(isSecondToken == true){
char* token2 = strtok(token1, ",");
while (token2) {
printf("token2: %s\n", token2);
token2 = strtok(NULL, " ");
}
}
token1 = strtok(NULL, " ");
isSecondToken = true;
}
}
Output I get:
token1: 0
token1: 1,2,3
token2: 1
token2: 2,3
Expected output:
token1: 0
token1: 1,2,3
token2: 1
token2: 2
token2: 3
When updating the token1 and token2 pointers you need to use the same token splitter:
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
int main(void){
char s[10];
strcpy(s, "0:1,2,3");
char* token1 = strtok(s, ":");
//To let me know it is on the second part
bool isSecondToken = false;
while (token1) {
printf("token1: %s\n", token1);
if(isSecondToken == true){
char* token2 = strtok(token1, ",");
while (token2) {
printf("token2: %s\n", token2);
token2 = strtok(NULL, ",");
}
}
token1 = strtok(NULL, ":");
isSecondToken = true;
}
}
Also strcpy requires the string.h library, so you were probably also getting some warnings of implicit declaration.
Related
I have a string "token" and an empty array of strings "arr". I want to add token to the first index of arr. I've tried arr[0][0] = token, but this would only work for chars and I've also tried arr[0] = token but this throws the error "expression must be a modifiable lvalue". My full program is:
#include <stdio.h>
#include <string.h>
char arr[100][100] = {};
char *token = strtok(StringToBeSplit, " ");
int i = 0;
while(token != NULL) {
arr[0] = token;
printf("%s\n", token);
token = strtok(NULL, " ");
i++;
}
What should I do?
You need to avoid assigning string literal to strtok. Basically using strcpy to copy the token to array and increment array as advised by previous answer. Something like this:-
#include <stdio.h>
#include <string.h>
int main(){
char arr[100][100] = {};
char str[] = "Split this string";
char sep[] = " ";
char *token = strtok(str, sep);
int i = 0;
while(token != NULL){
strcpy(arr[i], token);
token = strtok(NULL, sep);
i++;
}
//Test If it can print the strings
for(int j = 0; j < i; j++)
printf("%s\n", arr[j]);
}
Let's say I have a string containing integers "1 3 4 9" and I want to separate them based on the whitespace between them, and then save them into an array.
For example:
Input:
char str[] = "1 3 4 9";
int arr[4];
Then arr should be like:
arr[] = {1, 3, 4, 9}
How to do that in C, please help me. Hope I made the question clear
You can use strtok:
#include <stdio.h>
#include <string.h>
int main()
{
char str[] = "1 3 4 9";
char *token = strtok(str, " ");
while (token != NULL)
{
printf("%s\n", token);
token = strtok(NULL, " ");
}
return 0;
}
Your desired functionality in C.
#include <stdio.h>
#include <string.h>
int main()
{
char str[] = "1 3 4 9";
char newstr[50];
char *token = strtok(str, " ");
while (token != NULL)
{
printf("%s\n", token);
strcat(newstr, token);
token = strtok(NULL, " ");
}
printf("Newstr: %s",newstr);
return 0;
}
It will work with any string which will be including white space in it.
This question already has answers here:
Nested strtok function problem in C [duplicate]
(2 answers)
What are the differences between strtok and strsep in C
(3 answers)
Closed 5 years ago.
I have a function that takes a string. I make a copy of the string and a char *token;. I say token = strtok(myString, " "); and then start a while loop with while(token != NULL) {...}, and I do some stuff in the while loop. At the end of the while loop I say token = strtok(NULL, " "); which seems to follow examples I've seen online. My input string is "this is a string I will mess with" and my first token is "this" which is great, but then after I say token = strtok(NULL, " "); token appears to be "(null)" instead of "is". I am wondering why. Here is my code:
int numReps2(char *s) {
printf("inside numReps2 with \"%s\"\n", s);
// iterate through s until you find a space
// do matchingwords with the word and an accumulator string that you build char x char
int l = mystrlen(s);
char *copy = malloc(1+(sizeof(char) * l));
mystrcpy(copy, s);
char *accu = malloc(1+(sizeof(char) * l));
int ret = 0;
printf("about to tokenize \"%s\"\n", copy);
char *token;
const char delim[2] = " ";
token = strtok(copy, delim);
while(token != NULL) {
if(matchingWords(accu, copy)) {
printf("match-> accu is \"%s\" and token is \"%s\"\n", accu, token);
// we have a match
ret++;
} else {
// no match, add to accu
char *temp = mystrappend(token, " "); // stick a space after it
printf("no match, adding \"%s\" to \"%s\", token is still \"%s\"\n", temp, accu, token);
accu = mystrappend(accu, temp);
printf("accu is now \"%s\"\n", accu);
free(temp);
printf("after freeing temp, token is \"%s\"\n", token);
}
printf("here is what's after token: %c\n", *(token+strlen(token)+2));
token = strtok(NULL, delim);
printf("just tok'd again, now it's \"%s\"\n", token);
}
free(copy);
free(accu);
return ret;
}
I want to split strings received from the terminal input, if they are contained on a buffer. If they are I want to print them.
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* fich[5]={"ha","he","hi","ho","hu"};
int main(){
char passaarg[70];
const char space[2]= " ";
char *token;
int i;
while(1){
read(STDIN_FILENO, passaarg, 70);
for (i = 0; i < 5; i++){
if(strncmp(passaarg, fich[i], 2) == 0){
token = strtok(passaarg, space);
while(token != NULL){
printf("%s\n", token);
token = strtok(NULL, space);
printf("%s\n", token);
}
break;
}
}
}
return 0;
}
My output is the following one:
ha he
ha
he
he
Segmentation fault (core dumped)
I suspect your problem is here:
token = strtok(passaarg, space);
while(token != NULL){
printf("%s\n", token);
token = strtok(NULL, space);
printf("%s\n", token);
}
That second printf will cause undefined behavior (likely a crash) when strtok returns NULL, as it will when there are no more tokens in the string. Just remove that line.
Stylistically, I'd use a for loop here:
for(token = strtok(passaarg, space); token != NULL; token = strtok(NULL, space)) {
printf("%s\n", token);
}
while(token != NULL){
printf("%s\n", token);
token = strtok(NULL, space);
}
The while loop will fail when the token is NULL. At this time you are trying to print this pointer using your second printf() in the while loop which will lead to undefined behavior.
Get rid of your second printf()
I'm trying to split a string at spaces and tabs.
char * token = strtok(input, " \t");
works only for spaces. What am I doing wrong?
Here is an example that illustrates that strtok() will work on tabs or spaces.
The key is to pass in NULL on the all but the first call to strtok().
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[])
{
char buffer[1024];
int rv = sprintf(buffer, "a string\ttokenize.");
char *token = strtok(buffer, " \t");
int i = 0;
printf("cnt token\n");
printf("==========\n");
while (token) {
printf("%2d %s\n", i++, token);
token = strtok(NULL, " \t");
}
return 0;
}
output from above program is as follows below.
cnt token
==========
0 a
1 string
2 tokenize.