• C

String manipulation

I need a program that read a string from the user, then make a manipulation on the output, depending on the input.
For example, if I'll write "Hello" on the input line, the output will be "Hi", but it will remember each word of the input string, for me to output it somewhere else in the manipulated string. My idea was to make a function that convert string to array of words, but I had no luck, so far.
Who is Participating?

Improve company productivity with a Business Account.Sign Up

newmangConnect With a Mentor Commented:
You need to be a bit more explicit about what you want to do for a more comprehensive answer but here's something to go on with. I assume you want to split a string up into the individual words. To do this you can use the strtok function. This function will return pointers to each token delimited by a particular character or characters. Therefore if we use a space as the delimiting character this will give us a series of pointers to each word.

Note that the string being tokenised will be modified by the strtok calls by it placing NULLs at the end of each word so you may need to copy the string to a working copy if you want to retain the original string unmodified.

As an example here is a code snippet. You will have to work out how you want to hold the pointers, you may have a fixed size array or a linked list, it depends on how many words you expect.....

Lets assume the string input is sz_input. The code could be like this....

char * ptr_work_string; /* This will point to the work copy of the string */
char   tokens[]=" "; /* This is the word separator */
char * ptr_word;     /* This will point to the word */

/* Allocate enough memory to hold a copy of the input string */
if((ptr_work_string=(char *)malloc(strlen(sz_input)+1))==NULL)
     return FAILED; /* Malloc failed! */
/* Copy the input string. Normally we would use strncpy but we already know the destination is large enought to hold the source */

    /* Store the current word pointer somewhere */
    /* Advance to the next word */

do some other stuff with the pointers


This should give you something to go on with. Come back with more posts if you don't understand this.

Cheers - Gavin
I think what you mean is a linked-list of words ?
Try to be clearer...

tomercohenAuthor Commented:
Well, my idea is probably something like linked-list of words. Array can be a linked-list.
tomercohenAuthor Commented:
I've done it with the old-fusion way. Thanks anyway.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.