Given an array of characters, you must type a character and how many times it has been encountered.

For example: aaaaaaabbbbbbcccccdaaa. a 7 b 6 c 5 d 1 a 3

Here is my program, but it is going in cycles.

type mas=array[1..100] of char; var i,j,n,k:integer; x:mas; begin i:=0; repeat i:=i+1; read(x[i]); until x[i]='.'; n:=1; k:=1; while n<i do begin write(x[n],' '); if x[n] = x[Succ(n)] then while x[n] = x[Succ(n)] do begin n:=n+1; inc(k); end; writeln(k); end; end. 

    1 answer 1

    Julia, two while are not needed here, they confuse you. Write one for loop. Keep the previous symbol and number of repetitions in variables. Compare the current character with the previous one. If they are the same, increase the number of repetitions. If they are different, output the previous character and the accumulated number of repetitions, change the previous character to the current one, and set the number of repetitions to 1.