sorting problem

hello..
i have kined of noobie question..
if i have a string : "a,f,b,k,e"
and i want to convert it, so it will look like this : "e,k,b,f,a".
how can i do it??
(i think i have to move them to the right or left 5 times).
Basically i need to create a program that get an arry from the user and it shouldnt be more that 50 chars.
then do what i showed you above.
i tryed to make an other arry and do like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
using namespace std;
int main ()
{
int i,j,a;
char one[a],two[a];// dont shore this is correct.
do
{
cout<<"enter char less than 50.\n";
cin>>one[a];
}
while (a>50);
// i dont know how to set the valu of "a" ! 
// it should be the number of the chars in the arry.
// depends on what the user enter.
for (j=0;j<a;j++)
{
    for (i=0;i<a;i++)
    {
        two[i+1]=one[i];
    }
two[0]=one[a];
}
cout<<"now you got "<<two[a]<<endl;
system ("PAUSE");
return 0;
}

but it didn't work.
hlp me pls :)

~~~~~~~~~~~~~~~~~~~~~
Edit : ohhh i got a terible mistake! i cant do this by moving them 50 times to the right cuz its geting back to where it starts..
so i still need your help.. :D
Last edited on
Hi

I think you might be after something like what follows.

I assume that you want to reverse the string thats inputed and not the commas.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <string.h>

using	namespace	std;

int	main(int argc,char** argv)
{
	char	one[50];			//reserve space for 50 characteers
	char	two[50];			//

	cout << "enter less than 50 characters" << endl;

	cin >> one;				// all characters go into character array.

	int	length = strlen(one);		//length in Characters

	for (int i=0;i<length;i++)	
	{	
		two[length-i-1] = one[i];	 //Reverses Order
	}

	cout << "now you got " << two << endl; //output result
}


Hopefully this is a good example of what you wanted.
Shredded
thank you shredded.
but i think you use pointers..
and i want to solve this without pointers and strlen.
i'm still at the begining.

is there any option to do so?
thanks!
Last edited on
hi again,

Im not sure you can do without pointers of some type.

char one[50] is a pointer to an array of 50 characters.

You can get by without using strlen by doing something like this

1
2
3
4
5
6
7
8

    int     length = 0;

    while (one[length] != 0)        // 0 terminates character string
    {
          length++;
    }


Sorry I couldn't help more
Shredded
thank you alot!!!
Topic archived. No new replies allowed.