string manipulation

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Feb 2007
Posts: 40
Reputation: Shaabangbang is an unknown quantity at this point 
Solved Threads: 0
Shaabangbang Shaabangbang is offline Offline
Light Poster

string manipulation

 
0
  #1
Feb 25th, 2007
Hello, i have a question about string manipulation, i am supposed to write a function that takes a character array and returns a void, so far so good, but its supposed to reverse the array as output, for example, if the input is "Hello" the output is "olleH", here is where the problem comes, the function is not supposed to print the string in reverse, it actually reverses it in memory. this is where im stuck, i dont know how to reverse the string in memory, like i was thinking i could use strlen to find the length of the array, then use strcpy to copy it into a new blank array, and reverse that new array. but i dont know how to reverse the string in memory.

P.S. i dont want you to solve it for me, i just need help on how to do it, any help is greatly appriciated.
Reply With Quote Quick reply to this message  
Join Date: Oct 2005
Posts: 329
Reputation: Clinton Portis is on a distinguished road 
Solved Threads: 37
Clinton Portis's Avatar
Clinton Portis Clinton Portis is offline Offline
Posting Whiz

Re: string manipulation

 
0
  #2
Feb 25th, 2007
one simple strategy, would be to pass the char array into your function as a reference.. therefore any manipulations you perform on the char array will affect it directly in memory.

algorithmically, reversing the array is simple. create a new dynamic temporary char array of equal length. copy first element to last element, copy first element+1 to last element-1, copy first element+2 to last element-2 etc. Then, copy the temp array back to the original array.

that's all there is to it.
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 171
Reputation: Lazaro Claiborn is an unknown quantity at this point 
Solved Threads: 13
Lazaro Claiborn's Avatar
Lazaro Claiborn Lazaro Claiborn is offline Offline
Junior Poster

Re: string manipulation

 
0
  #3
Feb 25th, 2007
Originally Posted by Shaabangbang View Post
Hello, i have a question about string manipulation, i am supposed to write a function that takes a character array and returns a void, so far so good, but its supposed to reverse the array as output, for example, if the input is "Hello" the output is "olleH", here is where the problem comes, the function is not supposed to print the string in reverse, it actually reverses it in memory. this is where im stuck, i dont know how to reverse the string in memory, like i was thinking i could use strlen to find the length of the array, then use strcpy to copy it into a new blank array, and reverse that new array. but i dont know how to reverse the string in memory.

P.S. i dont want you to solve it for me, i just need help on how to do it, any help is greatly appriciated.
One obvious way is to use a for loop or while loop; example:

  1. void reverseString(const char *input, char *output, int buffer_len) {
  2. int counter = 0;
  3.  
  4. for (;buffer_len >= 0; buffer_len--)
  5. {
  6. ouput[counter] = input[buffer_len];
  7. counter++;
  8. }
  9. buffer_len[counter] = '\0';
  10. }

That is just one way but might help give you some ideas. If you decide to use the above function, it is important that buffer_len is equal or less than the size of input or it'll overwrite the stack or produce a buffer overflow.

Good luck, LamaBot
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,728
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 737
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: string manipulation

 
0
  #4
Feb 25th, 2007
>i dont know how to reverse the string in memory
How would you reverse "a"? Do nothing, clearly because the reverse of "a" is "a". What about "ab"? Mightn't you just swap the characters like this?
  1. char save = str[0];
  2. str[0] = str[1];
  3. str[1] = temp;
Keep adding more characters and see how you would swap them around to reverse the string until you find a pattern. That's your algorithm for an in-place string reversal.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,033
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 177
Aia's Avatar
Aia Aia is offline Offline
Postaholic

Re: string manipulation

 
0
  #5
Feb 25th, 2007
I used your question to see if I could do it. I know is not exactly what you are asking. If I understand correctly, you want a void function, and main is not void. However, I though to post my code as another, problably not so elegant, idea.

Some of the loops are just for testing and prove that the memory addresses are the same. The "NULL" can be avoided to swap at first address if you want.


  1. #include <stdio.h>
  2.  
  3. int main()
  4. {
  5. char hi[] ="Hello";
  6. char ih[sizeof(hi)];
  7. char *hi_ptr;
  8. size_t i;
  9. int length;
  10.  
  11. hi_ptr = hi;
  12.  
  13. /* to prove the memory addresses */
  14. for(i = 0; i < sizeof(hi) / sizeof(char); i++)
  15. {
  16. printf("Memory location of hi[%d] = %d, and in it lives %c\n", i, hi_ptr++, hi[i]);
  17. }
  18.  
  19. strcpy(ih, hi);
  20.  
  21. length = strlen(ih);
  22.  
  23. /* set the pointer again since it was moved displaying the preview addresses */
  24. hi_ptr = hi;
  25.  
  26. for(i = 0; i < sizeof(ih) / sizeof(char); i++)
  27. {
  28. *(hi_ptr + i) = ih[length - i]; /* swap happens here */
  29. }
  30.  
  31. putchar('\n');
  32.  
  33. /* to prove memory addresses are the same */
  34. for(i = 0; i < sizeof(hi) / sizeof(char); i++)
  35. {
  36. printf("Memory location of hi[%d] = %d, and in it lives %c\n", i, hi_ptr++, hi[i]);
  37. }
  38.  
  39. getchar();
  40. return(0);
  41. }

I'm going to try now using Narue suggestion.
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 40
Reputation: Shaabangbang is an unknown quantity at this point 
Solved Threads: 0
Shaabangbang Shaabangbang is offline Offline
Light Poster

Re: string manipulation

 
0
  #6
Feb 26th, 2007
hmm, i like clinton portis's idea, to create a new dynamic temporary char array of equal length. copy first element to last element, copy first element+1 to last element-1, copy first element+2 to last element-2 etc. Then, copy the temp array back to the original array.

but how would i code that? would it be something like this? [code]

void reverse (const char *array)
{
int i,

length = strlen (array);

for (i = 0; i <= length; i++){

strcpy (array, temparray);

ok, now here i dont know how to copy first element+1 to last element - 1, then first element+2 to last element - 2.

also, excuse the abysmal coding, im a first year student and this is my first time doing a high level language like C.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,033
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 177
Aia's Avatar
Aia Aia is offline Offline
Postaholic

Re: string manipulation

 
0
  #7
Feb 26th, 2007
This is another way I have been playing with.



  1. /*
  2.  * gnirts.c
  3.  *
  4.  * plays with the idea of reversing a given
  5.  * array of characters, in memory.
  6.  */
  7. #include <stdio.h>
  8.  
  9. void reverse_s(char *string);
  10.  
  11. int main(void)
  12. {
  13. char hi[]="Daniweb is the best";
  14. size_t index;
  15.  
  16. /* show me the content and memory location */
  17. for(index = 0; index < sizeof(hi) / sizeof(char); index++)
  18. {
  19. printf("Hi[%d] = %c living at %d memory address\n", index, hi[index], &hi[index]);
  20. }
  21.  
  22. /* reverse the array in memory */
  23. reverse_s(hi);
  24.  
  25. putchar('\n');
  26.  
  27. /* show me the content and memory location after upsetting the array :) */
  28. for(index = 0; index < sizeof(hi) / sizeof(char); index++)
  29. {
  30. printf("Hi[%d] = %c living at %d memory address\n", index, hi[index], &hi[index]);
  31. }
  32.  
  33. getchar();
  34. return(0);
  35.  
  36. }
  37.  
  38. /*
  39.  * reverse_s function
  40.  * parameter: a pointer to a string
  41.  *
  42.  * reverse a given string
  43.  */
  44. void reverse_s(char *string)
  45. {
  46. char save;
  47. size_t i;
  48.  
  49. int len = strlen(string);
  50.  
  51. for(i = 0; i < (len / 2) + 1; i++)
  52. {
  53. save = string[i];
  54. string[i] = string[len - i];
  55. string[len -i] = save;
  56. }
  57. }
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: string manipulation

 
0
  #8
Feb 26th, 2007
> ok, now here i dont know how to copy first element+1 to last element - 1
First of all, allocate an array of equal length.

  1. char *reversedString;
  2. reversedString = (char*)malloc(sizeof(char)*strlen(string)+1);
Now all you need to do is loop through each char. Use the array operators [] to reference each individual charactar.
  1. reversedString[i] = string[stringLength-i];
(Assuming that you've already set stringLength to equal the length of string.)

[edit] And don't forget the terminating null character \0
"Technological progress is like an axe in the hands of a pathological criminal."

All my posts may be freely redistributed under the terms of the MIT license.
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: string manipulation

 
0
  #9
Feb 26th, 2007
Originally Posted by joeprogrammer View Post
(Assuming that you've already set stringLength to equal the length of string.)
One additional note: I forgot to mention that the actual amount that stringLength will have to contain is the strlen of string-1, to compensate for arrays starting at element 0.
"Technological progress is like an axe in the hands of a pathological criminal."

All my posts may be freely redistributed under the terms of the MIT license.
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 40
Reputation: Shaabangbang is an unknown quantity at this point 
Solved Threads: 0
Shaabangbang Shaabangbang is offline Offline
Light Poster

Re: string manipulation

 
0
  #10
Feb 26th, 2007
Originally Posted by joeprogrammer View Post
>
  1. char *reversedString;
  2. reversedString = (char*)malloc(sizeof(char)*strlen(string)+1);
Now all you need to do is loop through each char. Use the array operators [] to reference each individual charactar.
  1. reversedString[i] = string[stringLength-i];
[edit] And don't forget the terminating null character \0
hmm, why did you put (char*) in brackets? also whats malloc? like what does the variable stand for? or are you calling by value?
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC