You have answers to the question of how to do this, which is why your path does not work.
First of all, you had a good idea: start with the first char in the line, count it (you forgot to include the counting code), delete all occurrences of the same char in the line. The idea is ineffective, but it will work. You are having trouble with this piece of code:
For i:=1 to (length(s)) do begin If (c=s[i]) then begin delete(s,i,1); end; end;
The problem is that Pascal will take the value of Length(s) when it sets the loop, but your code changes the length of the string by deleting characters (using delete(s,i,1) ). You end up looking at bad memory. The second problem is that i will move forward, it doesn't matter if it matches and removes char or not. That is why it is bad.
You are going to check i = 1,2,3,4,5 by looking for a . When i is 1, you will find a match, remove the first char, and your line will look like this:
Now you are testing i = 2, and this does not match, because s [2] = b. You just missed one a , and that the given a will contain another round in the array and force your algorithm to read it twice. The “fixed” algorithm will look like this:
i := 1; while i <= Length(s) do if (c=s[i]) then Delete(s,i,1) else Inc(i);
This is different: in this example, if I find a match at 1 , the cursor does not move, so it sees the second a . Also, because I'm using a while , not a for loop, I can't run into the possible details of implementing a for loop.
Your algorithm has another problem. After a loop that deletes all occurrences of the first char string in the string, you prepare the next loop using this code:
C: = s [1];
The problem is that if you feed this algorithm into a line of the form aa (length = 2, two identical characters), it will enter a loop, delete or occurrences of a (which turn s into EMPTY), and then try to read the first char of the EMPTY line .
Last word: your algorithm should process an empty input line, returning count = 0. Here's a fixed algorithm:
var s:string; i,count:integer; c:char; begin Readln(s); count:=0; while Length(s) > 0 do begin Inc(Count); c := s[1]; i := 1; while i <= Length(s) do begin If (c=s[i]) then delete(s,i,1) else Inc(i); end; end; Writeln(Count); Readln; end.