|
 |
|
 |
10-27-2005, 05:55 PM
|
#1 (permalink)
|
|
Registered User
Join Date: Oct 2005
Posts: 3
|
Reverse String in C (w/o STL or New Buffer)
I'm new to the language, and need help developing a C function which will reverse a string in place and meet the following criteria:
a) You cannot use the STL Library. B) You cannot allocate another buffer. (i.e. it must be reversed in place) C) You can only use ANSI C available functions.
Any help would be greatly appreciated.
|
|
|
10-27-2005, 06:27 PM
|
#2 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,720
|
one thing comes to mind
Code:
//C++
#include <iostream>
#include <string>
void reverse(std::string &str)
{
char tmp;
for(int i=str.size()-1, j=0; i > j; i--, ++j)
{
tmp=str[i];
str[i]=str[j];
str[j]=tmp;
}
}
int main(){
std::string foo("string");
std::cout << foo << std::endl;
reverse(foo);
std::cout << foo << std::endl;
return 0;
}
Code:
/* C */
#include <stdio.h>
#include <string.h>
void reverse(char str[])
{
char tmp;
int i = strlen(str)-1, j=0;
for(; i > j; i--, ++j)
{
tmp=str[i];
str[i]=str[j];
str[j]=tmp;
}
}
int main(){
char foo[] = "string";
printf("%s\n", foo);
reverse(foo);
printf("%s\n", foo);
return 0;
}
|
|
|
10-27-2005, 06:33 PM
|
#3 (permalink)
|
|
Registered User
Join Date: Oct 2005
Posts: 3
|
Thanks redhead. I was also considering using a single char variable to swap characters, but I thought that might violate not being able to "allocate another buffer." What do you think?
|
|
|
10-27-2005, 06:58 PM
|
#4 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,720
|
It's the only way, how would you ever remember what charactor you swaped out to begin with ?? Besides a single char isn't allocating extra space, on the other hand something like:
Code:
char * res_str = (char*) malloc(strlen(orig_str)*sizeof(char));
/* or */
char res_str[strlen(orig_str)];
would be allocating extra buffer space...
|
|
|
10-27-2005, 07:02 PM
|
#5 (permalink)
|
|
Registered User
Join Date: Oct 2005
Posts: 3
|
That makes sense to me. Thanks again!
|
|
|
11-08-2005, 02:37 PM
|
#6 (permalink)
|
|
Code Monkey
Join Date: Mar 2005
Posts: 56
|
Don't really need tmp.
Code:
tmp=str[i];
str[i]=str[j];
str[j]=tmp;
can be rewritten as
Code:
str[i] ^= str[j];
str[j] ^= str[i];
str[i] ^= str[j];
|
|
|
11-08-2005, 02:55 PM
|
#7 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,720
|
^= is Bitwise exclusive OR and assign, which means it will XOR the bit's between the two and assign the value at the same time ie:
Code:
bit_a = 10011010
bit_b = 01011010
bit_a ^= bit_b (bit_a = (10011010 ^ 01011010) => bit_a = 11000000 )
bit_b ^= bit_a (bit_b = (01011010 ^ 11000000) => bit_b = 10011010 )
bit_a ^= bit_b (bit_a = (11000000 ^ 10011010) => bit_a = 01011010 )
As you can see, by that logic it will swap the two of them, so if you're dealing with complete bitwise opreations, this is the way..
But on the other hand, will a STORE versus a XOR instruction be faster for the CPU ?
|
|
|
11-08-2005, 02:58 PM
|
#8 (permalink)
|
|
Code Monkey
Join Date: Mar 2005
Posts: 56
|
No, I believe | is OR, & is AND, and ^ is XOR.
|
|
|
11-08-2005, 03:01 PM
|
#9 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,720
|
Yeah I just noticed, and corrected the mistake
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
Similar Threads
|
| Thread |
Thread Starter |
Forum |
Replies |
Last Post |
|
From C to Java
|
HighterDK |
Java |
11 |
07-13-2004 07:15 PM |
|
edit?
|
anon |
Lounge |
10 |
11-21-2002 03:02 PM |
All times are GMT -8. The time now is 12:01 PM.
|
Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
|
 |
|