How do I break a string into several substrings so I can work with each of them separately using the C language?

Asked

Viewed 170 times

0

ex: Suppose the user typed "I’m cute". Then, I would work with "I", then with "I am" and last with "beautiful", separately.

  • show the code you have tried to do so far

3 answers

2

André Lins' answer is the most pertinent in terms of portability, but it is good to know that the function Strtok() is a considered function destructive because it modifies the first parameter passed in addition to not being thread-safe (There are options like strtok_r() and strtok_s() that mitigates some of the problems). The most suitable function to work for String Explosion is strsep() that although it does not belong to the standard, it is the least problematic and has some interesting advantages such as the use of several delimiters without much headache:

#include <stdio.h>
#include <string.h>

int main()
{
    char *string = strdup("Eu sou bonito,porque sim!Olha só.");
    const char del[] = " .,!";
    char *occor;

    while( (occor = strsep(&string,del)) != NULL )
        printf("%s\n",occor);

    return(0);
}

Each iteration returns a fraction of the string until the string has been consumed.

A usable implementation of this function can be found at this link: https://github.com/robertbachmann/openbsd-libc/blob/master/string/strsep.c

1

A solution using the strpbrk function, which is standard C, and is non-destructive.

#include <stdio.h>
#include <string.h>

int main(void)
{
  char teste[] = "aaa1bbb2ccc3ddd";
  char* ptr1 = teste;
  char* ptr2;

  while ((ptr2 = strpbrk(ptr1, "1234567890")) != NULL)
  {
    int size = ptr2 - ptr1;
    printf("%.*s\n", size, ptr1);
    ptr1 = ++ptr2;
  }

  if (ptr1 != NULL)
    printf("%s\n", ptr1);
}

Testing:

$./371718 
aaa
bbb
ccc

Remarks.

The "%. 5s n" format formats a string with a fixed size of 5 characters. For example, printf("%.5s", "1234567890"); will format "12345" on output.

The "%. *s n" format formats a string with a fixed size, with the size indicated by the parameter previous to the string pointer parameter. For example, in the command printf("%.*s", size, "1234567890"); the size of the formatted string will be indicated by the value of the variable "size".

  • @It Wasn’t Me: strpbrk(___, " "))

  • 1

    I didn’t understand the question...but there are 3 functions in C that can be used for these manipulations, and which are generally little known: strpbrk (never used), strspn and strcspn...with these 3 functions you can do this type of manipulation, but even so the whole thing is half-manual

  • I still don’t understand...I used numbers, but I could have used "xk9;z1," for example, just to illustrate that the strpbrk function uses a set of delimiters, not just 1 delimiter

  • 1

    @It Wasn’t Me: is it good or bad ? (I guess I never understand what you write)

  • Bye, that’s good, since it answers/answers!!!

  • from what I understood of the question typing is done inside the program, and not received as parameter in the command line, but to take from the command line is trivial, using the parameters argc and argv of the main function

  • @It Wasn’t Me: not "automatically" at the line of this command printf("%.*s\n", size, ptr1); we could save the substring for later use; suggest you try making a small test program (a "Poc") with your questions, and open a question

Show 2 more comments

0

Use the function strtok library string.h in the following way, this function returns a pointer to the first token found in the string, and each new interaction it returns the next token:

#include <stdio.h>
#include <string.h>

int main() {
  char text[] = "Eu sou bonito";
  char *textSplit;
  textSplit = strtok(text, " ");

  while (textSplit != NULL) {
    printf("%s\n", textSplit);
    textSplit = strtok(NULL, " ");
  }

  return 0;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.