Swapping character positions in an array.

So let's say I have an array named a[8] and i stored the characters 3386-481, from a[0] to a[7] respectively. How could I swap the positions of the hyphen and the 6, so that the output now looks like this: 338-6481. When I try to use the swap(a[3],a[4]) function it replaces the 6 with a hyphen and it outputs: 338--481. I wanted to know if there's a way to prevent this.

i cannot see the problem, can you post a simple code that generated the same problem you're having?
You are mistaken. Standard function std:;swap does the work.

std::swap( a[3], a[4] );
The hyphen is always supposed to be at number1[3], and it is supposed to switch with the number at number1[3] when it is not. However, if the input were to be FOOD-YUM, the output would be 366--986, when it should be 366-3986. That is the only issue i'm having.
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
 string number;
	char number1[8];
	int counter;
	char a='A',b='B',c='C',d='D',e='E',f='F',g='G',h='H',i='I',j='J',k='K',l='L';
	char m='M',n='N',o='O',p='P',r='R',s='S',t='T',u='U',v='V',w='W',x='X',y='Y';
	char hyphen='-';
	cout<<"Enter your phone-number: " <<endl;
	getline(cin,number);
	size_t findhyphen=number.find(hyphen);
	for(counter=0;counter<8;counter++)
	{
		number1[counter]=number[counter];

	}
	for(counter=0;counter<8;counter++)
	{
		if(number1[counter]=='1')
		{
			cout<<1;
		}
		if(number1[counter]=='0')
		{
			cout<<0;
		}
		if(number1[counter]==hyphen)
		{
			cout<<hyphen;
		}
		if((number1[counter]==a)||(number1[counter]==b)||(number1[counter])==c||(number1[counter]=='2'))
		{
			cout<<2;
		}
		if((number1[counter]==d)||(number1[counter]==e)||(number1[counter]==f)||(number1[counter]=='3'))
		{
			cout<<3;
		}
		if((number1[counter]==g)||(number1[counter]==h)||(number1[counter]==i)||(number1[counter]=='4'))
		{
			cout<<4;
		}
		if((number1[counter]==j)||(number1[counter]==k)||(number1[counter]==l)||(number1[counter]=='5'))
		{
			cout<<5;
		}
		if((number1[counter]==m)||(number1[counter]==n)||(number1[counter]==o)||(number1[counter]=='6'))
		{
			cout<<6;
		}
		if((number1[counter]==p)||(number1[counter]==r)||(number1[counter]==s)||(number1[counter]=='7'))
		{
			cout<<7;
		}
		if((number1[counter]==t)||(number1[counter]==u)||(number1[counter]==v)||(number1[counter]=='8'))
		{
			cout<<8;
		}
		if((number1[counter]==w)||(number1[counter]==x)||(number1[counter]==y)||(number1[counter]=='9'))
		{
			cout<<9;
		}
		if(findhyphen!=3)
		{
			swap(number1[3],number1[4]);

		}
One more please read what I already wrote. std::swap swaps two elements.

As for your stupid code

1
2
3
4
5
6
7
8
for(counter=0;counter<8;counter++)
{

	if(findhyphen!=3)
	{
		swap(number1[3],number1[4]);

	}


then it has nothing common with a problem of std::swap.
Last edited on
We all started somewhere. Anways, I fixed it.
Topic archived. No new replies allowed.