|
 |
|
 |
07-12-2003, 11:03 AM
|
#1 (permalink)
|
|
Code Monkey
Join Date: Mar 2003
Location: California
Posts: 57
|
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.
|
|
|
07-12-2003, 11:15 PM
|
#2 (permalink)
|
|
LOAD "*",8,1
Join Date: Feb 2003
Location: la.ca.us
Posts: 254
|
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--;
|
|
|
|
07-12-2003, 11:37 PM
|
#3 (permalink)
|
|
[code][/code] enforcer
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
|
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;
}
}
|
|
|
07-12-2003, 11:58 PM
|
#4 (permalink)
|
|
LOAD "*",8,1
Join Date: Feb 2003
Location: la.ca.us
Posts: 254
|
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?
|
|
|
07-13-2003, 12:16 AM
|
#5 (permalink)
|
|
[code][/code] enforcer
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
|
I made this proggy on the fly, habit wise.
The essence is nevertheless clear It hink  .
|
|
|
07-14-2003, 01:59 PM
|
#6 (permalink)
|
|
Registered User
Join Date: Mar 2003
Location: Netherlands
Posts: 11
|
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;
}
|
|
|
07-14-2003, 03:21 PM
|
#7 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,696
|
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.
|
|
|
07-14-2003, 05:10 PM
|
#8 (permalink)
|
|
[code][/code] enforcer
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
|
Don't worry about that. Just try to make own algorithms first.
At least learn things like strcpy. Then you'll move on  .
|
|
|
07-15-2003, 08:11 AM
|
#9 (permalink)
|
|
Registered User
Join Date: Mar 2003
Location: Netherlands
Posts: 11
|
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.)
|
|
|
| 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
|
|
|
All times are GMT -8. The time now is 11:22 PM.
|
Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
|
 |
|