![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Feb 2006
Posts: 18
Rep Power: 0
![]() |
reversing a string
i tried to reverse a string using arrays and for loops i the following program
[code] #include<iostream> #include<cstdlib> using namespace std; int main() { int i,j; cout<<"enter the array"; char arr[10],rr1[10]; for(i=0;i<10;i++) cin>>arr[i]; for(i=9;i>=0;i--) for(j=0;j<=9;j++) rr1[j]=arr[i]; for(j=0;j<=9;j++) cout<<rr1[j]; system("pause"); return 0; } [code] the reversed array is assigned 9 elements which are equal to the first element of arr. |
|
|
|
|
|
#2 |
|
Hobbyist Programmer
Join Date: Oct 2005
Location: Melbourne, Australia
Posts: 126
Rep Power: 4
![]() |
#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
int i,j;
cout<<"enter the array";
char arr[10],rr1[10];
for(i=0;i<10;i++)
cin>>arr[i];
for(i=9;i>=0;i--)
{
for(j=0;j<=9;j++)
rr1[j]=arr[i];
}
for(j=0;j<=9;j++)
cout<<rr1[j];
system("pause");
return 0;
}I have only done C, but I assume you need the braces around that for loop in C++ as well. And you don't actually have to use two arrays to complete this task either.
__________________
it's ironic considerate rarity patron of love higher knowledge engulfs me... |
|
|
|
|
|
#3 |
|
Programmer
Join Date: Jan 2006
Location: UK
Posts: 55
Rep Power: 3
![]() |
This should help, i code in C, I dont know much c++ but it should work
[PHP] #include <stdio.h> #include <string.h> int main() { char name[]="kieran"; int len=strlen(name)-1; int i=0; while(i<len) { name[i]^=name[len]; name[len]^=name[i]; name[i]^=name[len]; ++i; --len; } printf("%s\n", name); return 0; } [/PHP] :banana: |
|
|
|
|
|
#4 |
|
Professional Programmer
Join Date: Jun 2005
Location: India, The great.
Posts: 435
Rep Power: 4
![]() |
name[i]^=name[len];
name[len]^=name[i];
name[i]^=name[len];Why use ^ operator just to save a temporary variable? do this instead: char temp=name[len];
name[len]=name[i];
name[i]=temp;
__________________
PFO - My daily dose of technology. |
|
|
|
|
|
#5 |
|
Expert Programmer
Join Date: Jun 2005
Posts: 894
Rep Power: 4
![]() |
@biohazard: In your original code, you have this loop (reindented):
for(i=9;i>=0;i--)
for(j=0;j<=9;j++)
rr1[j]=arr[i];You don't need the inner loop, you just want to copy the value from the arr array to its reversed position in the rr1 array for(i=9;i>=0;i--)
rr1[9-i]=arr[i];@bivhitscar: you don't need braces around those for loops as they are only one statement. I would normally put braces there for clarity, but it is not required. |
|
|
|
|
|
#6 |
|
Newbie
Join Date: Jul 2004
Location: Blacksburg, VA
Posts: 13
Rep Power: 0
![]() |
void reverseString( char* in, char* out )
{
int inLength = strlen( in );
if( strlen( out ) >= inLength )
{
out[inLength] = '\0';
for( int i = inLength-1; i > -1; i-- )
out[i] = *in++;
}
} |
|
|
|
|
|
#7 | |
|
Programming Guru
![]() Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 6
![]() |
Quote:
2icgnyoi5hygfco8hyfbo857bcn37hfcw53o7fhbc357h357nfhc35t cg2589yu598y87fyb5c487fyh5o7fc5nhc85f8x45nh8x7f75h2 fcx2unx9825u98235y98fcy3b278f23rxyf7fh845n8743hyfc8w3yfrrsdjsegmufye
__________________
i put on my robe and wizard hat... Have you ever heard of Plato, Aristotle, Socrates?...Morons. |
|
|
|
|
|
|
#8 | |
|
Hobbyist Programmer
|
Quote:
__________________
When will Jesus bring the porkchops? |
|
|
|
|
|
|
#9 |
|
Newbie
Join Date: Feb 2006
Posts: 18
Rep Power: 0
![]() |
i didnt get u blood ninja
i didnt get u blood ninja.
do you mean that it prints a load of junk values |
|
|
|
|
|
#10 |
|
Hobbyist Programmer
|
Use code marks, It's ALOT easier to read and understand the code. Aspecially for those who are helping you with it.
__________________
When will Jesus bring the porkchops? |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|