Solved

String manipulation

Posted on 2001-06-05
4
376 Views
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.
0
Comment
Question by:tomercohen
  • 2
4 Comments
 
LVL 4

Accepted Solution

by:
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 */
strcpy(ptr_work_string,sz_input);

ptr_word=strtok(ptr_work_string,tokens);
while(ptr_word!=NULL)
{
    /* Store the current word pointer somewhere */
    this_bit_for_you_to_write.........
    /* Advance to the next word */
    ptr_word=strtok(NULL,token);
}

do some other stuff with the pointers

free(ptr_work_string);
return GOOD_FINISH;

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

Cheers - Gavin
0
 
LVL 2

Expert Comment

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

Yair
0
 

Author Comment

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

Author Comment

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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

730 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