[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 396
  • Last Modified:

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.
0
tomercohen
Asked:
tomercohen
  • 2
1 Solution
 
newmangCommented:
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
 
yairyCommented:
I think what you mean is a linked-list of words ?
Try to be clearer...

Yair
0
 
tomercohenAuthor Commented:
yairy,
 
Well, my idea is probably something like linked-list of words. Array can be a linked-list.
0
 
tomercohenAuthor Commented:
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!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now