• Javascript
  • Python
  • Go
Tags: string c

Splitting a String by Space in C

When it comes to working with strings in the C programming language, one common task is to split a string into smaller parts based on a cert...

When it comes to working with strings in the C programming language, one common task is to split a string into smaller parts based on a certain delimiter. The most commonly used delimiter is the space character, which is used to separate words in a sentence. In this article, we will explore different techniques for splitting a string by space in C.

Before we dive into the code, let's first understand the basic concept of strings in C. A string is simply a sequence of characters, terminated by the null character '\0'. So, when we say we want to split a string, what we actually mean is that we want to create multiple strings from a single string.

The most straightforward way to split a string by space is to use the C library function `strtok()`. This function takes two arguments - the string to be split and the delimiter. It returns a pointer to the first token (a substring) and modifies the original string by replacing the delimiter with a null character. Let's see an example:

```

#include <stdio.h>

#include <string.h>

int main(){

char str[] = "Splitting a String by Space in C";

char *token = strtok(str, " ");

while (token != NULL){

printf("%s\n", token);

token = strtok(NULL, " ");

}

return 0;

}

```

Output:

```

Splitting

a

String

by

Space

in

C

```

As you can see, the `strtok()` function splits the string at each space character and returns the words as tokens. The second argument of `strtok()` can be any character or a set of characters that you want to use as a delimiter.

But the problem with `strtok()` is that it modifies the original string, which may not always be desirable. To avoid this, we can use the `strndup()` function to create a copy of the original string and perform the splitting on the copy. Here's an example:

```

#include <stdio.h>

#include <string.h>

int main(){

char str[] = "Splitting a String by Space in C";

char *str_copy = strndup(str, strlen(str));

char *token = strtok(str_copy, " ");

while (token != NULL){

printf("%s\n", token);

token = strtok(NULL, " ");

}

free(str_copy);

return 0;

}

```

Output:

```

Splitting

a

String

by

Space

in

C

```

Another way to split a string by space is by using the `sscanf()` function. This function reads formatted input from a string and stores the results in the specified variables. We can use it to read words from a string, one by one, until we reach the end of the string. Here's an example:

```

#include <stdio.h>

#include <string.h>

int main(){

char str[] = "Splitting a String by Space in C";

char word[20];

int i = 0;

while (sscanf(str, "%s%n", word, &i) == 1){

printf("%s\n", word);

str += i;

}

return 0;

}

```

Output:

```

Splitting

a

String

by

Space

in

C

```

One thing to note about using `ss

Related Articles

C# Equivalent of C's sscanf

C# Equivalent of C's sscanf: A Powerful Tool for Efficient String Parsing In the world of programming, string parsing is an essential skill ...