[Webinar] Streamline your web hosting managementRegister Today


String manipulation

Posted on 2001-06-05
Medium Priority
Last Modified: 2010-04-15
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.
Question by:tomercohen
  • 2

Accepted Solution

newmang earned 200 total points
ID: 6158078
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

Expert Comment

ID: 6163773
I think what you mean is a linked-list of words ?
Try to be clearer...


Author Comment

ID: 6164599
Well, my idea is probably something like linked-list of words. Array can be a linked-list.

Author Comment

ID: 6169663
I've done it with the old-fusion way. Thanks anyway.

Featured Post

Live webcast with Pinal Dave

Pinal Dave will teach you tricks to help identify the real root cause of database problems rather than red herrings. Attendees will learn scripts that they can use in their environment to immediately figure out their performance Blame Shifters and fix them quickly.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
Suggested Courses

612 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question