split a string in c

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 887 Views - Last Post: 11 July 2020 - 07:47 PM Rate Topic: -----

#1 blackeagle123   User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 131
  • Joined: 28-November 13

split a string in c

Posted 27 June 2020 - 10:47 AM

i tried to search for it but this sounds pretty weird in c .
Suppose i have a string str "coding=c"
i want to split on"=" and get two strings coding as a first string and c as a second string is there any other function then strtok ?
Is This A Good Question/Topic? 0
  • +

Replies To: split a string in c

#2 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 7462
  • View blog
  • Posts: 25,113
  • Joined: 05-May 12

Re: split a string in c

Posted 27 June 2020 - 11:16 AM

Be aware that strtok() does not allocate space for any new strings. What it does is clobber the buffer that the original string was in to stick in null terminators, and then gives your pointers to the beginning of each the sequences.
Was This Post Helpful? 0
  • +
  • -

#3 blackeagle123   User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 131
  • Joined: 28-November 13

Re: split a string in c

Posted 28 June 2020 - 04:12 AM

Ok if you please could take a moment and look at my cod so far let me know if anything is wrong or need to be fixed but it seems to work fine make if you have somme suggestion on replacing some code with something else if better would be appreciated.
Some info on what i'm trying to do so i have a text file called text.txt that have some word=meaning multiple lines
i have an array of pointers that have the size 26 equal to the alphabet number
so while reading from text file suppose first line was abandoned=having been deserted or left then i should add to the list that have the pointer in t[0] so basicly all the words with a are in a list in t[0],all the words in b are in a list in t[1] ...
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

typedef struct cell {
    char mot[255];
    char signification[255];
    struct cell *suivant;
} cell;

void print(cell* l) {
    cell* p = l;
    while (p != NULL) {
        printf("%s\t", (*p).mot);
        printf("%s\n", (*p).signification);
        p = (*p).suivant;
    }
}

int main(int argc, char** argv) {
    cell * t[26];
    char alphabet[26] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
    for (int i = 0; i < 26; i++) {
        t[i] = NULL;
    }
    FILE* filePointer;

    char str[255];
    filePointer = fopen("test.txt", "r");

    while (fgets(str, 255, filePointer)) {

        str[strcspn(str, "\r\n")] = '\0';



        for (int i = 0; i < 26; i++) {
            if (tolower(str[0]) == alphabet[i]) {
                cell* p = (cell*) malloc(sizeof (cell));


                char *token = strtok(str, "=");
                char str1[1000];

                strlcpy(p->mot, token, 255);
                while (token != NULL) {

                    strlcpy(p->signification, token, 255);
                    token = strtok(NULL, "=");


                }
                p->suivant = t[i];
                t[i] = p;
                break;
            }

        }

    }
    print(t[0]);



    return 0;
}

Was This Post Helpful? 0
  • +
  • -

#4 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 7462
  • View blog
  • Posts: 25,113
  • Joined: 05-May 12

Re: split a string in c

Posted 28 June 2020 - 11:21 AM

In C, there is no need to cast the return value of malloc().
Was This Post Helpful? 1
  • +
  • -

#5 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 7462
  • View blog
  • Posts: 25,113
  • Joined: 05-May 12

Re: split a string in c

Posted 28 June 2020 - 11:27 AM

str1 does not seem to be used .
Was This Post Helpful? 1
  • +
  • -

#6 blackeagle123   User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 131
  • Joined: 28-November 13

Re: split a string in c

Posted 28 June 2020 - 11:29 AM

View PostSkydiver, on 28 June 2020 - 11:27 AM, said:

str1 does not seem to be used .

yeah sorry for this was trying something forgot to remove it
Was This Post Helpful? 0
  • +
  • -

#7 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 7462
  • View blog
  • Posts: 25,113
  • Joined: 05-May 12

Re: split a string in c

Posted 28 June 2020 - 11:29 AM

Lines 24-26 can be eliminated by changing line 22 to:
cell * t[26] = { 0 };


Was This Post Helpful? 1
  • +
  • -

#8 blackeagle123   User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 131
  • Joined: 28-November 13

Re: split a string in c

Posted 28 June 2020 - 11:32 AM

View PostSkydiver, on 28 June 2020 - 11:29 AM, said:

Lines 24-26 can be eliminated by changing line 22 to:
cell * t[26] = { 0 };



got it thanks so this sets all the elemets of t to NUll so all the pointers in the array becomes NULL without doing a for loop thanks
Was This Post Helpful? 0
  • +
  • -

#9 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 7462
  • View blog
  • Posts: 25,113
  • Joined: 05-May 12

Re: split a string in c

Posted 28 June 2020 - 11:34 AM

The for loop too determine the value of i so that you can use t[i] can be replaced with something like:
int i = tolower(str[0]) - 'a';


Was This Post Helpful? 0
  • +
  • -

#10 blackeagle123   User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 131
  • Joined: 28-November 13

Re: split a string in c

Posted 28 June 2020 - 11:36 AM

View PostSkydiver, on 28 June 2020 - 11:34 AM, said:

The for loop too determine the value of i so that you can use t[i] can be replaced with something like:
int i = tolower(str[0]) - 'a';




didnt quiet understand this would be great if you could show what code it's replacing and how it's supposed to work
Was This Post Helpful? 0
  • +
  • -

#11 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 7462
  • View blog
  • Posts: 25,113
  • Joined: 05-May 12

Re: split a string in c

Posted 28 June 2020 - 11:38 AM

Something doesn't make sense. If the maximum lengths of mot and signification are 254, respectively, then shouldn't you be reading in a line that is at most 512 (== 254*2 + 2) characters long?
Was This Post Helpful? 0
  • +
  • -

#12 blackeagle123   User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 131
  • Joined: 28-November 13

Re: split a string in c

Posted 28 June 2020 - 11:50 AM

wont change anything in my code but i agree ur right i'm just not used to this size thing everywhere in c
Was This Post Helpful? 0
  • +
  • -

#13 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 7462
  • View blog
  • Posts: 25,113
  • Joined: 05-May 12

Re: split a string in c

Posted 28 June 2020 - 12:37 PM

View Postblackeagle123, on 28 June 2020 - 02:36 PM, said:

didnt quiet understand this would be great if you could show what code it's replacing and how it's supposed to work


This:
for (int i = 0; i < 26; i++) {
    if (tolower(str[0]) == alphabet[i]) {
        cell* p = (cell*) malloc(sizeof (cell));


        char *token = strtok(str, "=");
        char str1[1000];

        strlcpy(p->mot, token, 255);
        while (token != NULL) {

            strlcpy(p->signification, token, 255);
            token = strtok(NULL, "=");


        }
        p->suivant = t[i];
        t[i] = p;
        break;
    }
}


is effectively the same as (assuming good input values):
int i;
for (i = 0; i < 26; i++) {
    if (tolower(str[0]) == alphabet[i]) {
        break;
    }
}

cell* p = (cell*) malloc(sizeof (cell));


char *token = strtok(str, "=");
char str1[1000];

strlcpy(p->mot, token, 255);
while (token != NULL) {

    strlcpy(p->signification, token, 255);
    token = strtok(NULL, "=");


}
p->suivant = t[i];
t[i] = p;



So you can replace:
int i;
for (i = 0; i < 26; i++) {
    if (tolower(str[0]) == alphabet[i]) {
        break;
    }
}


with
int i = tolower(str[0]) - 'a';



Obviously, you'll need to add a check to make sure that 0 <= i <= 26 so that bad input doesn't cause an out of array range access.
Was This Post Helpful? 1
  • +
  • -

#14 Aviral Vishwakarma   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 31-March 20

Re: split a string in c

Posted 11 July 2020 - 12:56 AM

View Postblackeagle123, on 27 June 2020 - 10:47 AM, said:

i tried to search for it but this sounds pretty weird in c .
Suppose i have a string str "coding=c"
i want to split on"=" and get two strings coding as a first string and c as a second string is there any other function then strtok ?

So write it as char integer

So write 1 string as char and one as into so in well be easay
Was This Post Helpful? 0
  • +
  • -

#15 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 7462
  • View blog
  • Posts: 25,113
  • Joined: 05-May 12

Re: split a string in c

Posted 11 July 2020 - 06:19 AM

You can't write a non-empty string as a single char in C.

Perhaps there is a trick that you want to show us with regards to having the other string as an integer. Care to share some code snippets demonstrating the idea/trick?

It's still unclear to me how this will help solve the problem of splitting the string on the equal sign using something built into the C language or standard library. Again, some code snippets to demonstrate would be helpful for us to understand what you are alluding to.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2