String manipulation

Posted on 2001-06-05
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 100 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

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How do I test for current date? 9 95
mixing C++ and C code elegantly 10 153
Problem to ASCII 1 166
xamarin c# deserialize Json containing nested object 2 81
Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.

863 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now