944,175 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 5548
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
Feb 25th, 2007
0

string manipulation

Expand 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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
Shaabangbang is offline Offline
41 posts
since Feb 2007
Feb 25th, 2007
0

Re: string manipulation

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.
Reputation Points: 237
Solved Threads: 117
Practically a Posting Shark
Clinton Portis is offline Offline
822 posts
since Oct 2005
Feb 25th, 2007
0

Re: string manipulation

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
Reputation Points: 11
Solved Threads: 13
Junior Poster
Lazaro Claiborn is offline Offline
171 posts
since Jan 2007
Feb 25th, 2007
0

Re: string manipulation

>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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Feb 25th, 2007
0

Re: string manipulation

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.
Aia
Reputation Points: 2224
Solved Threads: 218
Nearly a Posting Maven
Aia is offline Offline
2,304 posts
since Dec 2006
Feb 26th, 2007
0

Re: string manipulation

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.
Reputation Points: 10
Solved Threads: 0
Light Poster
Shaabangbang is offline Offline
41 posts
since Feb 2007
Feb 26th, 2007
0

Re: string manipulation

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. }
Aia
Reputation Points: 2224
Solved Threads: 218
Nearly a Posting Maven
Aia is offline Offline
2,304 posts
since Dec 2006
Feb 26th, 2007
0

Re: string manipulation

> 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
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006
Feb 26th, 2007
0

Re: string manipulation

(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.
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006
Feb 26th, 2007
0

Re: string manipulation

>
  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?
Reputation Points: 10
Solved Threads: 0
Light Poster
Shaabangbang is offline Offline
41 posts
since Feb 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Deleting characters from string
Next Thread in C Forum Timeline: Print Prime Numbers(using for loop)





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC