1

I have written a class such that is given below as shown that

 public class Countletter 
 {
    public static void main(String args[]) throws IOException
    {        
      String str = "muhammed"; 
      char[] Array = str.toCharArray();

     for(int i=0;i<8;i++)
     {
       int count=1;
       for(int j=i+1;j<8;j++)
       {
        if(Array[i]==(Array[j]))
        {
            count++;
        }
       }
    System.out.println(""+Array[i]+":"+count);
    }
  }
}

The output should be,

Input  : Muhammed

output : m=3
         u=1
         h=1
         a=1
         d=1

But my code print like

    output :  m:3
              u:1
              h:1
              a:1
              m:2
              m:1
              e:1
              d:1

anybody know where is my fault? and If anybody knows this logic please help me

Md Aslam
  • 149
  • 3
  • 4
  • 11
  • Can anybody give sample coding for it?Because i tried with lot of way but i cannot solve it please help – Md Aslam Aug 07 '14 at 14:06

6 Answers6

1

Basically your code is counting the frequency of each letter from that point on, because your loop does not care if the letter has been counted.

The answer linked in the comments uses a Map but if you don't want to use that for some reason, there are a few other methods. My first thought after map would be a char count array.

int counts = new int[26];                 //Only counting lowercase letters
for(int i=0; i<counts.size(); i++)
    counts[i] = 0;                        //initialize all to 0
int a = 'a';                              //get the int representation of the first lowercase letter
str = str.toLowerCase();
for(int i = 0; i<str.length; i++){
    int let = ((int)str.charAt(i))-a;     //find the appropriate index in the count
    counts[let]++;                        //increment that letters count
}
for(int i =0; i<counts.size(); i++){
    if(c > 0)
        print(char(i+a) + ": " + c);      //only print the letters that exist
}

This will yeild the output you want, albeit in alphabetical order.

Adam Yost
  • 3,616
  • 23
  • 36
1

The error is that loop does not skip already counted items for eg for m the outer loop executes when

i=0 and gives count 3 for positions 0,4,5
 i=4 and gives count 2 for positions 4,5
 i=5 and gives count 1 for position 5

to prevent them to be copied again you can replace them with a space or any special character as shown below.

public class Countletter 
 {
    public static void main(String args[]) throws IOException
    {        
      String str = "muhammed"; 
      char[] Array = str.toCharArray();

     for(int i=0;i<8;i++)
     {
         if(Array[i]!=' '){
          int count=1;
           for(int j=i+1;j<8;j++)
           {
            if(Array[i]==(Array[j]))
            {
             count++;
             Array[j]=' ';
            }
          }
        System.out.println(""+Array[i]+":"+count);
       }
    }
  }
}
user3260861
  • 149
  • 6
0

The problem here is your logic finds the count of letters whose count are already taken. It doesn't show the count of d since you created a loop which checks i < 7. The string is an 8 character string.

import java.io.IOException;

public class Countletter 
{
public static void main(String args[]) throws IOException
{        
  int flag = 0;
  String str = "muhammed"; 
  char[] Array = str.toCharArray();

 for(int i=0;i<8;i++)
 {
     flag = 0;

   for (int k = 0 ; k < i ; k++)
   {
       if (Array[k] == Array[i])
           {
           flag = 1;
           break;
           }
   }
   int count=1;

   for(int j=i+1;j<8;j++)
   {
    if(Array[i]==(Array[j]))
    {
        count++;
    }
   }
   if (flag != 1)
       System.out.println(""+Array[i]+":"+count);
  }
  }
}
capt.swag
  • 10,335
  • 2
  • 41
  • 41
0

You should use LinkedHashMap to count characters. LinkedHashMap keep order of character occurance.

    String str = "muhammed"; 
    char[] array = str.toCharArray();
    Map<Character, Integer> countMap = new LinkedHashMap<Character, Integer>();
    for(char c:array) {
        Integer cnt = countMap.containsKey(c) ? countMap.get(c) + 1 : 1;
        countMap.put(c, cnt);
    }
    for(Map.Entry<Character, Integer> entry: countMap.entrySet()) {
        System.out.println("" + entry.getKey() + ": " + entry.getValue());
    }

output:

m: 3
u: 1
h: 1
a: 1
e: 1
d: 1
0

This is a logical mistake.

You are taking first letter and checking how many time it is occurring again in the string, then taking second character counting how many time it is occurring again.

Same you are taking each and every character, instead if one character is occurring second time or third or whatever you are supposed to skip the character.

The below code will help you.

public class Countletter {
public static void main(String args[]) throws IOException {
    String str = "aaaabbbbabab";
    char[] Array = str.toCharArray();

    for (int i = 0; i < str.length(); i++) {
        int count = 1;
        boolean charCameAlready=check(Array,Array[i],i);
        if(charCameAlready==false){
            for (int j = i + 1; j < str.length(); j++) {
                if (Array[i] == (Array[j])) {
                    count++;
                }
            }
            System.out.println("" + Array[i] + ":" + count);
        }
    }
}

private static boolean check(char[] array, char c,int limit) {
    for(int i=0;i<limit;i++){
        if(array[i]==c){
            return true;
        }
    }
    return false;
}

}

Jagadeesh
  • 862
  • 7
  • 23
0

Although i would strongly recommend you to use a Map object as suggested above, here's what you could change in your code to make it work :

String str = "muhammed";
char[] Array = str.toCharArray();

List<String> countedLetters = new ArrayList<String>();

for (int i = 0; i < 8; i++) {
    int count = 1;
    if (!countedLetters.contains("" + Array[i])) {
        for (int j = i + 1; j < 8; j++) {
            if (Array[i] == Array[j]) {
                count++;
            }
        }
        countedLetters.add("" + Array[i]);
        System.out.println("" + Array[i] + ":" + count);
    }
}