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
Old 07-12-2003, 11:03 AM   #1 (permalink)
M3GAPL3X
Code Monkey
 
M3GAPL3X's Avatar
 
Join Date: Mar 2003
Location: California
Posts: 57
M3GAPL3X is on a distinguished road
Send a message via AIM to M3GAPL3X
Reversing String logic

I don't really understand what I am doing wrong with this function...basically what I want it to do is reverse a string (character by character).

Here is what I have so far:

Code:
void strReverse(char strng[])
{
	int i = 0, n, j=0;
	char reverse[80]; //the string that will reverse everything
	
	while (strng[i]) //while string isn't a null character '\0'
	i++; //increment i and store in string

	strng[i] = '\0';

	cout << strng << endl;
	
	n = strlen(strng); //find the length of the string after
	

	for (i=0; i < n; i++)
		for (j = n; j >= 0; j--)
		reverse[i] = strng[j];
		//i--;

	
	
	cout << "The reversed string is: " << reverse << endl;
}
Any help would be greatly appreciated.
__________________
--------------------------
M3GAPL3X@linuxmail.org
M3GAPL3X is offline   Reply With Quote
Old 07-12-2003, 11:15 PM   #2 (permalink)
joe_bruin
LOAD "*",8,1
 
Join Date: Feb 2003
Location: la.ca.us
Posts: 254
joe_bruin is on a distinguished road
Re: Reversing String logic

this code does nothing. why is it there?

Quote:
Originally posted by M3GAPL3X
Code:
	while (strng[i]) //while string isn't a null character '\0'
	i++; //increment i and store in string

	strng[i] = '\0';
the inner loop shouldn't be there. think about how the characters will be copied. strng[0] -> reverse[n - 1], strng[1] -> reverse[n - 2] and so on. you only need one loop for this.
also, remember to put the '\0' at the end of 'reverse'.

Quote:
Code:
	n = strlen(strng); //find the length of the string after
	

	for (i=0; i < n; i++)
		for (j = n; j >= 0; j--)
		reverse[i] = strng[j];
		//i--;
joe_bruin is offline   Reply With Quote
Old 07-12-2003, 11:37 PM   #3 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
Code:
// StrReversse.cpp : Entry point for this console app.
//Reversing a string.

/*The one and only core function swaps the first and last element of a char[], 
then it swaps the second element with the second last element. Etc.*/

#include "stdafx.h"
#include <iostream.h>
#include <string.h>

//Reversing a string

//Forward declaration.
void reverse(char* s);

int main(int argc, char* argv[])
{
	cout<<"enter a sentence"<<endl;
	char szSentence[100];
	cin.getline(szSentence,100);

	reverse(szSentence);
	cout<< "Reversed string: "<<szSentence<<endl;

	return 0;
}

void reverse(char* s)
{
	char temp; //help variable for "swapping" 2 characters.

	int lenght = strlen(s);
	int lastchar = lenght - 1;

	for(int i=0;i<lenght/2; i++)
	{
		temp=s[i];
		s[i]=s[lastchar-i];
		s[lastchar-i]=temp;
	}

}
Valmont is offline   Reply With Quote
Old 07-12-2003, 11:58 PM   #4 (permalink)
joe_bruin
LOAD "*",8,1
 
Join Date: Feb 2003
Location: la.ca.us
Posts: 254
joe_bruin is on a distinguished road
Quote:
Originally posted by Valmont
Code:
#include "stdafx.h"
#include <iostream.h>
#include <string.h>
what could a commandline string reverse program possibly need 'stdafx' for?
joe_bruin is offline   Reply With Quote
Old 07-13-2003, 12:16 AM   #5 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
I made this proggy on the fly, habit wise.
The essence is nevertheless clear It hink .
Valmont is offline   Reply With Quote
Old 07-14-2003, 01:59 PM   #6 (permalink)
Unicorn
Registered User
 
Unicorn's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 11
Unicorn is on a distinguished road
Writing a function to reverse a string is a good way to get familiar with the language. In future projects however, you might want to use the reverse function from the standard C++ library:

Code:
#include <iostream>
#include <algorithm>
#include <cstring>

using namespace std;

int main (int argc, char **argv)
{
  char sentence[100];
  cin.getline (sentence, 100);
  reverse (sentence, sentence + strlen (sentence));
  cout << sentence << endl;
  
  return 0;
}
Unicorn is offline   Reply With Quote
Old 07-14-2003, 03:21 PM   #7 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,696
redhead is on a distinguished road
What ISO describes the reverse() function ?? and when looking at the code, I can't seem to figure out, why the second argument should be: sentence + strlen (sentence) shouldn't this be something like strlen (sentence)

I would guess the first one, would return the string which gets the length of it appended in the end.. An argument I dont see any use for in any function.
__________________
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 07-14-2003, 05:10 PM   #8 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
Don't worry about that. Just try to make own algorithms first.
At least learn things like strcpy. Then you'll move on .
Valmont is offline   Reply With Quote
Old 07-15-2003, 08:11 AM   #9 (permalink)
Unicorn
Registered User
 
Unicorn's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 11
Unicorn is on a distinguished road
Quote:
Originally posted by redhead
What ISO describes the reverse() function ??
That would be ISO/IEC IS 14882.

Quote:
... and when looking at the code, I can't seem to figure out, why the second argument should be: sentence + strlen (sentence) shouldn't this be something like strlen (sentence)
In most STL functions, a range is used instead of a starting point and a distance. So the first argument is the starting point, and the second argument should point right after the last element of the thing you want to reverse. (Remember "sentence" is the address of the first character.)
Unicorn 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
Reversing a string and palindromes. Valmont Standard C, C++ 21 10-27-2004 07:28 AM
Help for another program Androto Standard C, C++ 54 10-15-2004 07:21 AM
From C to Java HighterDK Java 11 07-13-2004 07:15 PM


All times are GMT -8. The time now is 11:22 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