hello

i sopuse to make this string = "eeevv"
into this string = 3e2v
why its not working for me? thank u

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
#include<iostream>
using namespace std;
char chaf(char *ch){
	char s = *ch;
	char str[20];
	int n = 0;
	int lengh = 0;
	while(*ch != '\0'){
	while(*ch = s){
	lengh++;
	ch++;
	}
	str[n] = lengh + 48;
	n++;
	str[n] = s;
	lengh = 0;
	n++;
	s = *ch;
	}
	ch = &str[0];
	return *ch;
}
main(){
	char x[] = {'e','e','e','v','v','v','\0'};
	chaf(&x[0]);
	cout << x;
	return 0;
}
You have 3 v's so the output will be: 3e3v.
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
#include <iostream>
#include <cstring>

using namespace std;

char chaf(char *ch)
{
	char* start = ch;		// need to remember where the string starts
	int n = 0;
	char str[20];

	char s = *ch;
	int lengh = 0;

	while (*ch != '\0')
	{
		while (*ch == s)	// changed assignment to equality test
		{
			lengh++;
			ch++;
		}

		str[n++] = lengh + '0';
		str[n++] = s;

		s = *ch;
		lengh = 0;
	}

//	ch = &str[0];	// copy is incorrect
	memcpy(start, str, n);	// copy result over input
	start[n] = 0;	// terminate the string
	return *ch;		// not needed
}

main()
{
	char x[] = {'e','e','e','v','v','v','\0'};
//	chaf(0);	// Need to pass in x
	chaf(x);
	cout << x;
	return 0;
}
Last edited on
thank u!! the bigest mistak of me was the while (*ch = s) insted of while(*ch == s)
here is my new progrem:
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
#include<iostream>
using namespace std;
char chaf(char *ch){
	int lengh = 0;
	int n = 0;
	int b = 0;
	char *real = ch;
	char str[20];
	char s = *ch;
	while(*ch != '\0'){
		while(*ch == s){
			lengh++;
			ch++;
			}
		str[n] = lengh + 48;
		n++;
		str[n] = s;
		n++;
		lengh = 0;
		s = *ch;
	}
	while(*real != '\0'){
		*real = str[b];
		b++;
		real++;
	}
	
	}
	

main(){
	char x[] = "eeevvssss";
	chaf(x);
	cout << x;
	return 0;
}
Topic archived. No new replies allowed.