0

in my code below:

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



int main() {


    char text[256];

// three character pointers
    char *new_line;

// store new_line pointer with hello
    new_line = strcpy(text, "hello how are you");

    while(*new_line)
        printf("%c",*new_line++);



    return (0);
}

I am trying to divide the array pointed by *new_line based on the space in between the words stored in the array. more specifically I want to assign index to each word in the array and display it like:

1- hello
2- how
3- are
4- you

How can I break this char array and get these words individually?

Arnold
  • 185
  • 1
  • 2
  • 8

1 Answers1

0

You replace the first space after each word with a null character. The first letter of each word has a pointer to it for that word. This is the mothod used by the standard library function strtok.

caveman
  • 1,755
  • 1
  • 14
  • 19