| | |
string manipulation
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Feb 2007
Posts: 40
Reputation:
Solved Threads: 0
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.
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 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.
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.
•
•
•
•
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.
C Syntax (Toggle Plain Text)
void reverseString(const char *input, char *output, int buffer_len) { int counter = 0; for (;buffer_len >= 0; buffer_len--) { ouput[counter] = input[buffer_len]; counter++; } buffer_len[counter] = '\0'; }
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
>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?
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.
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?
C Syntax (Toggle Plain Text)
char save = str[0]; str[0] = str[1]; str[1] = temp;
I'm here to prove you wrong.
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.
I'm going to try now using Narue suggestion.
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.
C Syntax (Toggle Plain Text)
#include <stdio.h> int main() { char hi[] ="Hello"; char ih[sizeof(hi)]; char *hi_ptr; size_t i; int length; hi_ptr = hi; /* to prove the memory addresses */ for(i = 0; i < sizeof(hi) / sizeof(char); i++) { printf("Memory location of hi[%d] = %d, and in it lives %c\n", i, hi_ptr++, hi[i]); } strcpy(ih, hi); length = strlen(ih); /* set the pointer again since it was moved displaying the preview addresses */ hi_ptr = hi; for(i = 0; i < sizeof(ih) / sizeof(char); i++) { *(hi_ptr + i) = ih[length - i]; /* swap happens here */ } putchar('\n'); /* to prove memory addresses are the same */ for(i = 0; i < sizeof(hi) / sizeof(char); i++) { printf("Memory location of hi[%d] = %d, and in it lives %c\n", i, hi_ptr++, hi[i]); } getchar(); return(0); }
I'm going to try now using Narue suggestion.
•
•
Join Date: Feb 2007
Posts: 40
Reputation:
Solved Threads: 0
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.
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.
This is another way I have been playing with.
C Syntax (Toggle Plain Text)
/* * gnirts.c * * plays with the idea of reversing a given * array of characters, in memory. */ #include <stdio.h> void reverse_s(char *string); int main(void) { char hi[]="Daniweb is the best"; size_t index; /* show me the content and memory location */ for(index = 0; index < sizeof(hi) / sizeof(char); index++) { printf("Hi[%d] = %c living at %d memory address\n", index, hi[index], &hi[index]); } /* reverse the array in memory */ reverse_s(hi); putchar('\n'); /* show me the content and memory location after upsetting the array :) */ for(index = 0; index < sizeof(hi) / sizeof(char); index++) { printf("Hi[%d] = %c living at %d memory address\n", index, hi[index], &hi[index]); } getchar(); return(0); } /* * reverse_s function * parameter: a pointer to a string * * reverse a given string */ void reverse_s(char *string) { char save; size_t i; int len = strlen(string); for(i = 0; i < (len / 2) + 1; i++) { save = string[i]; string[i] = string[len - i]; string[len -i] = save; } }
> 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.
Now all you need to do is loop through each char. Use the array operators [] to reference each individual charactar.
(Assuming that you've already set stringLength to equal the length of
[edit] And don't forget the terminating null character \0
First of all, allocate an array of equal length.
C Syntax (Toggle Plain Text)
char *reversedString; reversedString = (char*)malloc(sizeof(char)*strlen(string)+1);
C Syntax (Toggle Plain Text)
reversedString[i] = string[stringLength-i];
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.
All my posts may be freely redistributed under the terms of the MIT license.
"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.
All my posts may be freely redistributed under the terms of the MIT license.
•
•
Join Date: Feb 2007
Posts: 40
Reputation:
Solved Threads: 0
•
•
•
•
>
Now all you need to do is loop through each char. Use the array operators [] to reference each individual charactar.C Syntax (Toggle Plain Text)
char *reversedString; reversedString = (char*)malloc(sizeof(char)*strlen(string)+1);
[edit] And don't forget the terminating null character \0C Syntax (Toggle Plain Text)
reversedString[i] = string[stringLength-i];
![]() |
Similar Threads
- Help Please - with String Manipulation (C++)
- VBScript string Manipulation Help (ASP)
- C String Manipulation (C)
- Erroneus outputs in string manipulation simple program (C++)
- leak using c++ string (C++)
Other Threads in the C Forum
- Previous Thread: Project help
- Next Thread: Factorial of a number
| Thread Tools | Search this Thread |
#include * ansi append array arrays asterisks bash binarysearch centimeter changingto char character convert copyimagefile cprogramme creafecopyofanytypeoffileinc createprocess() database dynamic execv fgets file floatingpointvalidation fork framework function getlogicaldrivestrin givemetehcodez grade gtkwinlinux hacking histogram ide inches include infiniteloop initialization input interest intmain() iso kernel keyboard kilometer km license linked linkedlist linux list lists looping lowest matrix meter microsoft number oddnumber open opendocumentformat openwebfoundation overwrite owf pdf pointer pointers posix power probleminc process program programming radix recursion recv recvblocked research reversing segmentationfault sequential single socket socketprogramming standard strchr string suggestions systemcall test testing threads turboc unix urboc user variable wab whythiscodecausesegmentationfault windowsapi






