Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4019614
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T10:07:34+00:00 2026-05-20T10:07:34+00:00

I have to write program that counts how many different letters are in string.

  • 0

I have to write program that counts how many different letters are in string.
For example “abc” will give 3; and “abcabc” will give 3 too, because there are only 3 different letters.

I need to use pascal, but if you can help with code in different languages it would be very nice too.

Here is my code that does not work:

var s:string;
    i,j,x,count:integer;
    c:char;
begin
  clrscr;

  Readln(s);
  c:=s[1];
  x:=1;

  Repeat
  For i:=1 to (length(s)) do
  begin
    If (c=s[i]) then
    begin
      delete(s,i,1);
      writeln(s);
    end;
  end;
  c:=s[1];
  x:=x+1;
  Until length(s)=1;

  Writeln(x);

x is the different letter counter;
Maybe my algorythm is very bad.. any ideas? Thank you.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T10:07:35+00:00Added an answer on May 20, 2026 at 10:07 am

    You’ve got answers on how to do it, here’s why your way doesn’t work.

    First of all intuitively you had a good idea: Start with the first char in the string, count it (you forgot to include the counting code), remove all occurrences of the same char in the string. The idea is inefficient, but it would work. You ran into trouble with this bit of code:

    For i:=1 to (length(s)) do
    begin
      If (c=s[i]) then
      begin
        delete(s,i,1);
      end;
    end;
    

    The trouble is, Pascal will take the Length(s) value when it sets up the loop, but your code changes the length of the string by removing chars (using delete(s,i,1)). You’ll end up looking at bad memory. The secondary issue is that i is going to advance, it doesn’t matter if it matched and removed an char or not. Here’s why that’s bad.

    Index:  12345
    String: aabbb
    

    You’re going to test for i=1,2,3,4,5, looking for a. When i is 1 you’ll find a match, remove the first char, and your string is going to look like this:

    Index:  1234
    String: abbb
    

    You’re now testing with i=2, and it’s not a match, because s[2] =b. You just skiped one a, and that given a is going to stay in the array an other round and cause your algorithm to count it twice. The “fixed” algorithm would 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 the given example, if I found a match at 1, the cursor doesn’t advance, so it sees the second a. Also because I’m using a while loop, not a for loop, I can’t get in trouble with possible implementation details of the for loop.

    Your algorithm has an other problem. After the loop that removes all occurrences of the first char in string you’re preparing the next loop using this code:

    c:=s[1];

    The trouble is, if you feed this algorithm an string of the form aa (length=2, two identical chars), it’s going to enter the loop, delete or occurrences of a (those turning s into an EMPTY string) and then attempt to read the first char of the EMPTY string.

    One final word: Your algorithm should handle the empty string on input, returning an count=0. Here’s the 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.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to write a program used internally where different users will have different
I have to write a program that read from a file that contains the
I have a Perl program, that needs to use packages (that I also write).
There are many ways to write a Python program that computes a histogram. By
I am using VB .NET to write a program that will get the words
I'm attempting to write a program in Linux using C++ that counts the number
I have a problem for a university lab; Write a short program that outputs
I have a program that writes to a FILE *cgiOut and just after it
I have two (UNIX) programs A and B that read and write from stdin/stdout.
Imagine we have a program trying to write to a particular file, but failing.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.