chfxin
chfxin

Reputation: 43

Cut string at a certain character

Im trying to make a function which cuts and prints/does something with parts of a string.

Say i have this string:

"strings.no.header"

And i would like to split it up so it prints something like this:

strings
no
header

Here is my lousy attempt, where i am first able to (without the else statement) print out "strings". My idea was to make the function recursive so it prints and removes the first part before ".", and then does the same with the rest of the string.

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

void cut(char string[]);

int main() {
  char string[] = "strings.no.header";
  cut(string);
}

void cut(char string[]) {
  char *toString = malloc(sizeof(char));
  int len = strlen(string);

  for(int i = 0; i < len; i++) {
    int count = 0;
    if(string[i] != '.') {
      toString[i] = string[i];
    } else if(string[i] == '.') {
      char *tmp = malloc(sizeof(char));
      tmp = string + i;
      cut(tmp);
    }
  }
  printf("%s", toString);
}

Would be grateful if someone could point me in the right direction.

Upvotes: 1

Views: 3286

Answers (4)

Nik
Nik

Reputation: 1870

As others have noted, for something like this, strtok will give you what you need. A word of warning, this function will wreck your string, so it is always advisable to make a copy first.

Basically, what strtok does is replace all characters in the string that match the second argument of strtok and replace them with the '\0' character. The function then returns the pointer to the first "token". Each subsequent call to strtok will return the pointer to the next token. Here is a basic tutorial.

To keep it simple, I kept all the code in the main. You can now use this technique to implement the logic that suits your particular problem.

int main()
{
    char string[] = "strings.no.header";
    char buffer[50] = { 0 };//make sure big enough
    char* token = NULL;
    char* nextToken = NULL;

    //want to make a copy because strtok will wreck the string
    strcpy(buffer, string);

    token = strtok_s(buffer, ".", &nextToken);
    while (token)
    {
        printf("%s\n", token);//or do whatever you like.
        token = strtok_s(NULL, ".", &nextToken);//pass NULL on subsequent calls
    }
    getchar();
    return 0;
} 

You may find this interesting that:
After the first call of token = strtok_s(buffer, ".", &nextToken);,
buffer now contains: "strings\0no.header".

After the second call, buffer now contains: "strings\0no\0header".

This is the reason why you want to make a copy of the original string before using strtok. If you do not care about the original string, it is ok to use it.

Upvotes: 1

hancar
hancar

Reputation: 797

A solution with strtok:

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

void cut(char string[]);

int main()
{

  char string[] = "strings.no.header";
  cut(string);

}


void cut(char string[])
{
    char *pch;
    pch = strtok (string, ".");
    while (pch != NULL)
    {
        printf ("%s\n",pch);
        pch = strtok (NULL, ".");
    }

}

Upvotes: 1

Gnqz
Gnqz

Reputation: 3382

Strtok is your friend. No need to reinvent the wheel.

void cut (const char* str){
  char * pch;
  pch = strtok (str,".");
  while (pch != NULL)
  {
    printf ("%s\n",pch);
    pch = strtok (NULL, ".");
  }
}

The code is just a quick edit of the sample from the link.

Upvotes: 1

Iria
Iria

Reputation: 497

I have an idea:

check this web page: https://en.wikibooks.org/wiki/C_Programming/Strings

use strchr to find '.'then, once you know the position, copy the characters in other string, remember that you already know where the '.' appears, so you know exactly what to copy.

Upvotes: 1

Related Questions