simple character string copy

Can somebody point me what is wrong with the following piece of code ?:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
  #include <iostream>
using namespace std;

int main()
{
    char  ch[] = "hello77o";
    char ch2[sizeof(ch)];
	 
	for(unsigned int i  = 0; i < sizeof(ch)-1 ;i++){
	while(ch[i]) ch2[sizeof(ch) - i -2 ] = ch[i];
     
	}
	
	cout<<ch2;

	return 0;

	
}

Last edited on
Take another look at line 10 after the = . . . . what is ch2[i] at this point?
Last edited on
ohhhhhh changed it ......
http://cpp.sh/44ga
still ....no ouput !
vs 2010 is giving a output with girbish ?
oh this solved the problem -->

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 #include <iostream>
using namespace std;

int main()
{
    char  ch[] = "hello77o";
    char ch2[sizeof(ch)];
	 
	for(unsigned int i  = 0; i < sizeof(ch)-1 ;i++){
	while(ch[i]) ch2[sizeof(ch) - i -2 ] = ch[i];
     
	}
	ch2[sizeof(ch)-1] = ch[sizeof(ch)-1];
	cout<<ch2;

	return 0;

	
}
Topic archived. No new replies allowed.