Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums

Go Back   Code Forums > Application and Web Development > Standard C, C++

Reply
 
LinkBack Thread Tools Display Modes
Old 10-27-2005, 05:55 PM   #1 (permalink)
bwhiteman
Registered User
 
Join Date: Oct 2005
Posts: 3
bwhiteman is on a distinguished road
Question 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.
bwhiteman is offline   Reply With Quote
Old 10-27-2005, 06:27 PM   #2 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,720
redhead is on a distinguished road
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;
}
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 10-27-2005, 06:33 PM   #3 (permalink)
bwhiteman
Registered User
 
Join Date: Oct 2005
Posts: 3
bwhiteman is on a distinguished road
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?
bwhiteman is offline   Reply With Quote
Old 10-27-2005, 06:58 PM   #4 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,720
redhead is on a distinguished road
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...
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 10-27-2005, 07:02 PM   #5 (permalink)
bwhiteman
Registered User
 
Join Date: Oct 2005
Posts: 3
bwhiteman is on a distinguished road
That makes sense to me. Thanks again!
bwhiteman is offline   Reply With Quote
Old 11-08-2005, 02:37 PM   #6 (permalink)
QUantumAnenome
Code Monkey
 
Join Date: Mar 2005
Posts: 56
QUantumAnenome is on a distinguished road
Send a message via Yahoo to QUantumAnenome
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];
QUantumAnenome is offline   Reply With Quote
Old 11-08-2005, 02:55 PM   #7 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,720
redhead is on a distinguished road
^= 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 ?
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 11-08-2005, 02:58 PM   #8 (permalink)
QUantumAnenome
Code Monkey
 
Join Date: Mar 2005
Posts: 56
QUantumAnenome is on a distinguished road
Send a message via Yahoo to QUantumAnenome
No, I believe | is OR, & is AND, and ^ is XOR.
QUantumAnenome is offline   Reply With Quote
Old 11-08-2005, 03:01 PM   #9 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,720
redhead is on a distinguished road
Yeah I just noticed, and corrected the mistake
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


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.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting