I created a function to split a string with comma.
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
char splitted_line[8][50];
void split(char *line){
printf("line 9\n");
char *part = strtok(line, ",");
printf("line 11\n");
for(int i=1; i<8; i++){
strcpy(splitted_line[i], part);
printf("%s\n", splitted_line[i]);
part = strtok(NULL, ",");
}
}
int main(){
char *line = "123,456,789";
split(line);
return 0;
}
but the result after running is :
line 9
Segmentation fault (core dumped)
it seems the problem is in char *part = strtok(line, ","); but I don't know what's that.
strtok() will modify passed original string directly.
You must not modify string literals.
char *line = "123,456,789";
should be modifyable array
char line[] = "123,456,789";
Also don't forget to check if part is not NULL before doing strcpy(splitted_line[i], part);.
Related
I'm doing an exercice where I need to split a string into an array of strings. The number of delimiters is checked before (the code snippet posted is a stripped down version however it doesn't work too), then the string is transformed into lowercase and it gets split into 4 parts separated by the delimiter "-". Here's the code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#define MAX_USERNAME_LENGHT 256
#define NUMBER_OF_ELEMENTS 4
void StringToArrayOfStrings(char *string, char **string_array, char *delimiter);
void LowerString(char * string, int string_lenght);
int main() {
char string[MAX_USERNAME_LENGHT] = "Joseph-Lucy-Mike-Nick"; //Test string
char *string_array[NUMBER_OF_ELEMENTS]; //We need four elements
char delimiter[] = "-";
int counter = 0;
//LowerString(string, strlen(string));
//printf("%s", string);
StringToArrayOfStrings(string, string_array, delimiter);
//Print each element of the string array
for (counter = 0; counter < NUMBER_OF_ELEMENTS; counter++) {
printf("\n%s\n", string_array[counter]);
}
return 0;
}
void LowerString(char * string, int string_lenght) {
unsigned short int counter;
for (counter = 0; counter < string_lenght; counter++) {
string[counter] = tolower(string[counter]);
}
}
void StringToArrayOfStrings(char *string, char **string_array, char *delimiter) {
unsigned short int counter;
char *token;
token = strtok(string, delimiter);
while(token != NULL) {
string_array[counter++] = token;
token = strtok(NULL, delimiter);
}
}
I've been scratching my head for the past 2 hours and I wasn't able to fix it. This programs works only if the string is not printed or/and transformed in lowercase. The program crashes when entering the loop in StringToArrayOfStrings. Where's the problem?
Thanks.
I want to split a string by the comma and separate the first number in the string into its own new string, the rest of the string I want to keep together.
So far I have tried this by using strtok() and I can get the first number into its own string, but now I can't figure out how to keep the rest of the string together.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
char testStr[] = "1000,first,second,third,+abc";
char *uidStr;
char *restOfstr;
int n;
//This is wrong, I know, but I want to populate
//the rest of the string after the first comma
//into a single string without the UID.
uidStr = strtok(testStr, ",");
while (n < 5)
{
restOfstr = strtok(NULL, ",");
n++;
}
return 0;
}
strtok works fine, you have to keep in mind that it returns a pointer to each tokenized word so you need two pointers one for the first token and other for the rest of the string.
Demo
#include <stdio.h>
#include <string.h>
int main()
{
char testStr[] = "1000,first,second,third,+abc";
char *uidStr; //pointer to uid
char *restOfstr; //pointers to the rest of the string
uidStr = strtok(testStr, ","); //uid remains in testStr
restOfstr = strtok(NULL, "\n"); //rest of the string
puts(uidStr); //or puts(testStr) to print uid
puts(restOfstr); //print rest of the string
return 0;
}
If you want a more secure function you can use strtok_s.
You can use strchr to find the first comma in the string.
Then using strncpy to get the number in the string.
The complete code:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main()
{
char *str = "1000,first,second,third,+abc";
char *s = strchr(str, ',');
if(!s)
return -1;
char num[10];
strncpy(num, str, s-str);
num[s-str] = '\0';
int a = strtol(num, NULL, 10);
printf("num = %d\nthe remaining: %s\n", a, s+1);
return 0;
}
#include <string.h>
#include <stdio.h>
int main(int ac, char **av) {
while (--ac) {
char *p = *++av;
char *t = strtok(p, ",");
char *r = strtok(NULL,"");
printf("%s : %s\n", t, r);
}
return 0;
}
Note that the empty string "" passed to the second strtok means that it cannot find a deliminator, thus returns the rest of the string.
In addition to the excellent answers #mevets and #anastaciu have provided (I would go with these), this code will also work fine.
#include <string.h>
#include <stdio.h>
int main(int argc, char** argv) {
char _p[] = "1000,Hey,There";
char* str1 = strtok(_p, ",");
char* str2 = strtok(NULL, "");
return 0;
}
#include <string.h>
#include <stdio.h>
int main()
{
char str[255] = "Hello;thisnewwolrd";
int i =0;
while(str[i] != ';')
{
i++;
}
i++;
char *name = NULL;
while(str[i] != NULL)
{
name[i] = str[i];
i++;
printf("%c \r\n",name[i]);
}
}
the expected output is thisnewwolrd but i am getting error of core dumped
canany one have reason why and how to over come this
This should work:
int main()
{
char str[255] = "Hello;thisnewwolrd";
char *ptr = strchr(str, ';') + 1;
char name[255];
strcpy( name, ptr);
printf("%s \r\n", name);
}
You don't have to reinvent the wheel, and are much better off using standard library functions for string manipulation.
You have to allocate memory to store your string copy. For example: char *name = malloc(255*sizeof(char));.
And you have to create another iterator than i to start to fill the memory space pointed by name starting at the index 0.
I'm getting a seg fault every time the code reaches the first strtok
token = strtok(commandLine," ");
I'm just trying to parse stdin and store it, using a space as a delimiter. A lot of problems I saw were people using strtok on a string literal, which I assume also applies to my case as well, but how do I work around that?
Thanks.
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
int main(int argc, char* argv[]){
//Used for parsing
char commandLine[255];
char* tokens[10];
char* token;
int counter;
int i;
printf("gets to pt 1\n");
//Parsing
while( fgets(commandLine, 255, stdin) ){
printf("\n%s\n", commandLine);
token = strtok(commandLine," ");
printf("gets here");
counter = 0;
for(counter = 0; token != NULL; counter++){
strcpy(tokens[counter], token);
token = strtok(NULL, " ");
}
}
printf("gets to point2");
for(i = 0; tokens[i] != NULL; i++){
printf("%s ", tokens[i]);
}
EDIT:
Here is the working code.
As User93353 pointed out, I had to allocate memory for my tokens so I changed
char* tokens[10]
to
char tokens[10][100]
and my for loop wasn't ending correctly, had to change
tokens[i] != NULL
to
i<counter
-
int main(int argc, char* argv[]){
//Used for parsing
char commandLine[255];
char tokens[10][100];
char* token;
int counter;
int i;
printf("gets to pt 1\n");
//Parsing
while( fgets(commandLine, 255, stdin) ){
printf("\n%s\n", commandLine);
token = strtok(commandLine," ");
printf("gets here");
for(counter = 0; token != NULL; counter++){
strcpy(tokens[counter], token);
token = strtok(NULL, " ");
}
printf("gets to printing");
for(i = 0; i<counter; i++){
printf("%s", tokens[i]);
}
}
}
Allocate memory for each element of the tokens array.
Easy way is to declare it as
#define SOME_SIZE 100
char tokens[10][SOME_SIZE];
Otherwise, tokens[0], tokens[1] etc point to some random location in memory. strcpying to that random location is causing your program to crash.
I get a segmentation fault the second time malloc runs:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int keywords_init(char *str, char ***str_arr);
int main(void) {
char str[] = "keyword1,keyword2,keyword3";
char **str_arr = NULL;
int arr_elements;
arr_elements = keywords_init(str, &str_arr);
return 0;
}
int keywords_init(char *str, char ***str_arr) {
int i;
char *pch;
/* String break */
pch = strtok(str, ",");
for (i = 0; pch != NULL; i++) {
*str_arr = realloc (*str_arr, (i+1)*sizeof(char *));
*str_arr[i] = malloc (strlen(pch) + 1);
strcpy(*str_arr[i], pch);
printf("%d: %s\n", i, pch);
pch = strtok (NULL, ",");
}
return i;
}
What confuses me is that if I don't pass the address of str_arr to keywords_init and use a double pointer instead of a triple one in keywords_init it works just fine.
You're getting bitten by operator precedence/associativity - change both occurrences of:
*str_arr[i]
to:
(*str_arr)[i]