0

I need to split a sentence contained in a char pointer into an array of words. I've tried several methods (as I cannot use strtok. The following method I used extracts the different words, but it overwrites word already stored in the previous index in the array. What can I do? Please help.

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <cs50.h>
#include <ctype.h>
#include <strings.h>

int main(int argc, string argv[])
{

if (argc != 2)
   {
        printf("please write a sentence\n");
        return 1;
   }
   int i = 0, j = 0, n = 0;
   char* words = argv[1];
    char word[20];
    string s[4];


   while (words[i] != '\0')
   {
                // ' '  is the separator to split words 
                if (words[i] == '#')
                {
                        word[j] = '\0';

                        s[n] = word;

                        j = 0;
                       n++;
                       memset(word, 0, sizeof(word));  
                }
                else {
                        word[j++] = words[i];
                }
                i++;
        }

        word[j] = '\0';



         /*for(int a = 0; a< n; a++)
          printf("%s\n", s[a]);*/

}
1

I'll try to give you a visualization of what anfauglit explained.

This is what your program is doing:
https://goo.gl/4MfFx4 (click the link, wait some seconds for it to load and then keep clicking the forward button).

And this is what you wanted your program to do, which probably needs to use malloc() so you can avoid making s[n] keep pointing again and again to the same block of memory labeled word:
https://goo.gl/n0LG4l

1
  • Thanks a lot! I get it now.
    – excel
    Nov 16 '16 at 3:53
0

You declared a character array char word[20] and array of pointers string s[4]each of them pointing to a character. (string data type is actually a pointer to a character).

Now, inside the loop you are filling the word array with characters from input string. When you encounter a '#' character you append a null terminator character to the word array. s[n] = word assigns a pointer to word (more accuratly, it assigns a pointer to the first element of word) to the first element of the array string s[4]. So far so good.

On the second run of your loop your writing to the same old word array starting at index 0, so you are effectivly overwritting what was there before. After you reach the end of the second word in the input string, you assign to the second element of string s[4] a pointer to the word again!

As the result, all your items in string s[4] array will point to the same array word and the contect of that array will be what you wrote to it recently.

2
  • Thanks for responding promptly to this post. I actually did not fully understand exactly what you mean. Would you mind expressing what you were trying to explain in code or better still help me with a better way of achieving my aim. Thank you!
    – excel
    Nov 15 '16 at 17:40
  • It feels like you are trying to approach a problem that is beyond your current C knowledge. Instead of trying to understand this particular example and making it work, invest your time in reading about pointers and memory management in C (especially about dynamic memory allocation)
    – anfauglit
    Nov 15 '16 at 21:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .