Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 14th, 2006, 11:46 PM   #1
biohazard
Newbie
 
biohazard's Avatar
 
Join Date: Feb 2006
Posts: 18
Rep Power: 0 biohazard is on a distinguished road
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.
biohazard is offline   Reply With Quote
Old Mar 15th, 2006, 12:08 AM   #2
bivhitscar
Hobbyist Programmer
 
bivhitscar's Avatar
 
Join Date: Oct 2005
Location: Melbourne, Australia
Posts: 126
Rep Power: 4 bivhitscar is on a distinguished road
#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...
bivhitscar is offline   Reply With Quote
Old Mar 15th, 2006, 12:48 AM   #3
AICkieran
Programmer
 
Join Date: Jan 2006
Location: UK
Posts: 55
Rep Power: 3 AICkieran is on a distinguished road
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:
AICkieran is offline   Reply With Quote
Old Mar 15th, 2006, 1:38 AM   #4
InfoGeek
Professional Programmer
 
InfoGeek's Avatar
 
Join Date: Jun 2005
Location: India, The great.
Posts: 435
Rep Power: 4 InfoGeek is on a distinguished road
        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.
InfoGeek is offline   Reply With Quote
Old Mar 15th, 2006, 2:57 AM   #5
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 894
Rep Power: 4 The Dark is on a distinguished road
@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];
The inner for loop (on j) is performed for each iteration of the outer loop (on i). The inner for loop is setting all values of the rr1 array to the value of arr[i]. This means that in the end all values of rr1 are going to be equal to arr[0] (as that is the last value of i used).
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.
The Dark is offline   Reply With Quote
Old Mar 15th, 2006, 3:25 AM   #6
CodeWeasel
Newbie
 
Join Date: Jul 2004
Location: Blacksburg, VA
Posts: 13
Rep Power: 0 CodeWeasel is on a distinguished road
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++;
	}
}
Only stipulation with using this code is that you should back up the original string if you want to use it again.
CodeWeasel is offline   Reply With Quote
Old Mar 18th, 2006, 1:34 AM   #7
bl00dninja
Programming Guru
 
bl00dninja's Avatar
 
Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 6 bl00dninja is on a distinguished road
Quote:
#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;
}
jucgbrshfrshf387yto8p42389r7234b9rpyr7yoc87y23l7yf7iofhnwerhfh4roifh45w
2icgnyoi5hygfco8hyfbo857bcn37hfcw53o7fhbc357h357nfhc35t
cg2589yu598y87fyb5c487fyh5o7fc5nhc85f8x45nh8x7f75h2
fcx2unx9825u98235y98fcy3b278f23rxyf7fh845n8743hyfc8w3yfrrsdjsegmufye
__________________
i put on my robe and wizard hat...

Have you ever heard of Plato, Aristotle, Socrates?...Morons.
bl00dninja is offline   Reply With Quote
Old Mar 18th, 2006, 1:40 AM   #8
Nebula
Hobbyist Programmer
 
Nebula's Avatar
 
Join Date: Oct 2005
Posts: 205
Rep Power: 4 Nebula is on a distinguished road
Send a message via AIM to Nebula
Quote:
Quote:
#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;
}


jucgbrshfrshf387yto8p42389r7234b9rpyr7yoc87y23l7yf 7iofhnwerhfh4roifh45w
2icgnyoi5hygfco8hyfbo857bcn37hfcw53o7fhbc357h357nf hc35t
cg2589yu598y87fyb5c487fyh5o7fc5nhc85f8x45nh8x7f75h 2
fcx2unx9825u98235y98fcy3b278f23rxyf7fh845n8743hyfc 8w3yfrrsdjsegmufye
Point made.
__________________
When will Jesus bring the porkchops?
Nebula is online now   Reply With Quote
Old Mar 18th, 2006, 8:50 AM   #9
biohazard
Newbie
 
biohazard's Avatar
 
Join Date: Feb 2006
Posts: 18
Rep Power: 0 biohazard is on a distinguished road
i didnt get u blood ninja

i didnt get u blood ninja.
do you mean that it prints a load of junk values
biohazard is offline   Reply With Quote
Old Mar 18th, 2006, 11:30 AM   #10
Nebula
Hobbyist Programmer
 
Nebula's Avatar
 
Join Date: Oct 2005
Posts: 205
Rep Power: 4 Nebula is on a distinguished road
Send a message via AIM to Nebula
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?
Nebula is online now   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
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
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 6:42 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC