String Reversal

I need to reverse a string in C

Page 1 of 1

12 Replies - 2703 Views - Last Post: 09 November 2007 - 02:56 PM Rate Topic: -----

#1 McKeenicus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 16-August 07

String Reversal

Posted 16 August 2007 - 11:02 AM

Hey Guys,

I hope this hasn't been specifically covered. I tried my best to track down something similar to this but nothing for the finite parameters I have to work with.

I've been given a question from a friend and I haven't been able to solve it on my own. It has been requested that I learn how to "Reverse a String without using C++ or the STL". I've managed to break it down to having to use pointer arithmetic however I'm at a loss for how to specifically set this algorithm up. The several times I've tried, without any though have been completed both in C++...which negates the test of this question.

For reference: This is not a homework question. I've already finished an 18 month course in programming however it has been over a year since I've done anything with it and I am currently trying to brush off the dust. I appreciate any help that can be provided and once again I do apologize if this has been posted, specifically, somewhere else.

This post has been edited by McKeenicus: 16 August 2007 - 11:09 AM


Is This A Good Question/Topic? 0
  • +

Replies To: String Reversal

#2 Topher84  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 113
  • View blog
  • Posts: 359
  • Joined: 04-June 07

Re: String Reversal

Posted 16 August 2007 - 11:07 AM

You have to show some coded effort before anybody is going to help you out "code wise."

If that isn't what you are after may i suggest : http://www.google.com


But if you have some code already please post it and we will be glad to help :)
Was This Post Helpful? 0
  • +
  • -

#3 no2pencil  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5363
  • View blog
  • Posts: 27,325
  • Joined: 10-May 07

Re: String Reversal

Posted 16 August 2007 - 11:19 AM

As per Topher84's suggestion, http://www.google.co...G=Google+Search
Wow, look at all the relevant results!

Notice that the 1st one is even DreamInCode.net?

Best of luck to you.
Was This Post Helpful? 0
  • +
  • -

#4 McKeenicus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 16-August 07

Re: String Reversal

Posted 16 August 2007 - 11:25 AM

It appears I misunderstood the purpose of these forums. I found the answer on google. I'll make sure to keep my future posts to code postings only.

This post has been edited by McKeenicus: 16 August 2007 - 11:29 AM

Was This Post Helpful? 0
  • +
  • -

#5 skyhawk133  Icon User is offline

  • Head DIC Head
  • member icon

Reputation: 1876
  • View blog
  • Posts: 20,284
  • Joined: 17-March 01

Re: String Reversal

Posted 16 August 2007 - 11:45 AM

We are here to help, but for simple tasks like string reversals, we really have to require everyone show us what they've done so far otherwise we would just write code for everyone all day and nobody would learn anything.

Even if your question wasn't related to homework, it's still not something we can just provide code for in the forums. There are examples in our code snippets area that you could use to get you started and then we'd be happy to help with any errors you're getting. But we're not really here to just write code.

Can we assume you want to do this in C and not C++ since you've already done it in C++?

Do you have any code at all?
Was This Post Helpful? 0
  • +
  • -

#6 Topher84  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 113
  • View blog
  • Posts: 359
  • Joined: 04-June 07

Re: String Reversal

Posted 16 August 2007 - 11:46 AM

:ph34r: heh what are the odds that searching for what you want on google will lead you to a DIC snippet?
Was This Post Helpful? 0
  • +
  • -

#7 McKeenicus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 16-August 07

Re: String Reversal

Posted 16 August 2007 - 11:53 AM

No, no fair enough. I understand what you're saying and I do agree with it. I managed to find an example in C through google.

All my code was in C++, my problem with doing this algorithm in C was that I had no clue where to start. All my solutions kept coming up in C++. All I had to start with was a semi-clear question and the knowledge to use pointer arithmetic.

#include <stdio.h>
#include <string.h>

int main(void)
{
  char str1[] = "Reverse this string using C";
  char str2[27], *p1, *p2;

  /* make p point to end of str1 */
  p1 = str1 + strlen(str1) - 1;

  p2 = str2;

  while(p1 >= str1)
	*p2++ = *p1--;

  /* null terminate str2 */
  *p2 = '\0';

  printf("%s %s", str1, str2);

  return 0;
}



This is what I've got, and as far as I know it works. It seems logically correct, I just haven't had a chance to test this out in an IDE yet.

This post has been edited by McKeenicus: 16 August 2007 - 11:55 AM

Was This Post Helpful? 0
  • +
  • -

#8 skyhawk133  Icon User is offline

  • Head DIC Head
  • member icon

Reputation: 1876
  • View blog
  • Posts: 20,284
  • Joined: 17-March 01

Re: String Reversal

Posted 16 August 2007 - 11:58 AM

Quote

heh what are the odds that searching for what you want on google will lead you to a DIC snippet?



In case you weren't being sarcastic

You want the "duh" answer or the actual answer?

The "duh" answer is... if you're here, it's VERY likely you'd find what you want on google ;)

The actual is, also, very likely because we have over 100,000 pages in google and many of those rank for several variations and search terms.

This is one reason I'm not really a fan of sending people back to google since over 80% of the 40,000 visitors we get daily arrived from google.
Was This Post Helpful? 0
  • +
  • -

#9 Topher84  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 113
  • View blog
  • Posts: 359
  • Joined: 04-June 07

Re: String Reversal

Posted 16 August 2007 - 12:06 PM

No.. i wasn't being sarcastic.. i was just surprised when i searched for "1D Arrays" and the snippet i wrote was the 3rd result on google for DIC.. thought it was neat.
Was This Post Helpful? 0
  • +
  • -

#10 kingsalami  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 09-November 07

Re: String Reversal

Posted 09 November 2007 - 02:20 PM

-using strlen or any length function is an immediate sign of weakness.

-creating an array is not a solution, it limits you to a certain size. That sucks.

-this is essentially a pointer arithmatic question.

-elegance and brevity should be strived for.

-I think recursion here is a poor solution, as you use more memory and cpu cycles by throwing function calls on the stack and then unwinding the stack to reverse.

-this function prints the reversed string. Any thing can be substituted here instead of a cout, you could store it etc...

void Reverse(char* str)
{
	char* p = str;

	while(*p != '\0') ++p;

	do
	{
		--p;
		cout << *p;
	}while(p != str);
}



If the code doesn't make sense to you all it does is creates a pointer that moves to the end of the string and then prints the chars one at a time from the end back to the front until the memory locations are the same and then it stops.

Also as a bonus you can reverse your string in place with NO more memory by using a few OR's on you chars, no temp storage is needed.
Was This Post Helpful? 0
  • +
  • -

#11 no2pencil  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5363
  • View blog
  • Posts: 27,325
  • Joined: 10-May 07

Re: String Reversal

Posted 09 November 2007 - 02:47 PM

View Postkingsalami, on 9 Nov, 2007 - 02:20 PM, said:

-using strlen or any length function is an immediate sign of weakness.

Can you explain why?

View Postkingsalami, on 9 Nov, 2007 - 02:20 PM, said:

-creating an array is not a solution, it limits you to a certain size. That sucks.

If you know the length of the string, & you are doing an exact data swap, then you already know your limitation.
Was This Post Helpful? 0
  • +
  • -

#12 jjhaag  Icon User is offline

  • me editor am smartastic
  • member icon

Reputation: 45
  • View blog
  • Posts: 1,789
  • Joined: 18-September 07

Re: String Reversal

Posted 09 November 2007 - 02:52 PM

Quote

while(*p != '\0') ++p;

This is basically what strlen() does. So how exactly is writing more code to do the exact same thing a weakness?


Quote

-elegance and brevity should be strived for.

This wins for most banal comment. Of course you should strive for that. However, the point of doing an assignment is to learn - that's first priority.

This post has been edited by jjhaag: 09 November 2007 - 02:53 PM

Was This Post Helpful? 0
  • +
  • -

#13 no2pencil  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5363
  • View blog
  • Posts: 27,325
  • Joined: 10-May 07

Re: String Reversal

Posted 09 November 2007 - 02:56 PM

View Postkingsalami, on 9 Nov, 2007 - 02:20 PM, said:

Also as a bonus you can reverse your string in place with NO more memory by using a few OR's on you chars, no temp storage is needed.

With 3+ghz of processing power, & Memory storage 2+gig, do you really need to spend extra time trimming down a program that swaps text? How efficient should a function be that proves racecar is truly a palindrome?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1