DEV Community

Neelakandan R
Neelakandan R

Posted on

count of letter

1) Frequency of Each letter in a given String(key != '*')
2) Non-repeated char. in a given String(count==1)
3) Repeated Char. in a given String(count>1)
4) First Repeated Char. in a given String(count>1) use break(First Repeated Char)
5) Last repeated char. in a given String(char last=' ');

ex :
if ((key != '*')&&(count>1))// if condition don't print repeated letter
{
last=key;

}

6) First Non-repeated char. in a given String(count==1)---break;
7) Last non-repeated char. in a given String(char last=' ');

ex :
if ((key != '*')&&(count==1))// if condition don't print repeated letter
{
last=key;

}

8) Most frequent letter in a given String(code at last)
9)finding same letter or numb near by near(letter)

count of letter (or) Frequency of Each letter in a given String

package afterfeb13;

public class findchar {
    public static void main(String[] args) {

        char[] s = { 'n', 'e', 'e', 'l', 'a', 'k', 'a', 'n', 'd', 'a', 'n' };

        for (int j = 0; j < s.length; j++)// selects each character from the array one by one.
        {
            char key = s[j];// s[j] represent all char because j++
            int count = 1;// frist char
            for (int i = j+1; i < s.length; i++) //Why j + 1?// Avoid Repetition: you don’t want to compare it with itself,
                //and you don’t want to count the same 'n' multiple times. 


            {
                if (key == s[i]) {
                    s[i] = '*';//mark for repeated letter
                    count++;// inside each how many time
                }
            }
            if (key != '*')// if condition don't print repeated letter
                System.out.println(key + " appears " + count);//print letter not repeated
        }
    }

}

Enter fullscreen mode Exit fullscreen mode

Output:
n appears 3
e appears 2
l appears 1
a appears 3
k appears 1
d appears 1

convert string to char and find Frequency of Each letter in a given String

package afterfeb13;

public class findchar {
    public static void main(String[] args) {

        String c = "neelakandan";
        char[] s=c.toCharArray();// string to char


        for (int j = 0; j < s.length; j++)// selects each character from the array one by one.
        {
            char key = s[j];// s[j] represent all char because j++
            int count = 1;// frist char
            for (int i = j+1; i < s.length; i++) //Why j + 1?// Avoid Repetition: you don’t want to compare it with itself,
                //and you don’t want to count the same 'n' multiple times. 


            {
                if (key == s[i]) {
                    s[i] = '*';//mark for repeated letter
                    count++;// inside each how many time
                }
            }
            if (key != '*')// if condition don't print repeated letter
                System.out.println(key + " appears " + count);//print letter not repeated
        }
    }

}



Enter fullscreen mode Exit fullscreen mode

Output:
n appears 3
e appears 2
l appears 1
a appears 3
k appears 1
d appears 1

change 1 :if ((key != '*')&&(count==1)) char present one time(non duplicate char)

Output:
l appears 1
k appears 1
d appears 1

change 2 :if ((key != '')&&(count==3)) char present three time(duplicate char))(if ((key != '')&&(count>1)))

Output:
n appears 3
e appears 2
a appears 3

change 3 :if ((key != '*')&&(count==1)) {break;}char present one time and first char(non duplicate char) break should apply one char

Output:l appears 1

find last char

package afterfeb13;

public class findchar {
    public static void main(String[] args) {

        String c = "neelakandan";
        char last=' ';
        char[] s=c.toCharArray();// string to char


        for (int j = 0; j < s.length; j++)// selects each character from the array one by one.
        {
            char key = s[j];// s[j] represent all char because j++
            int count = 1;// frist char
            for (int i = j+1; i < s.length; i++) //Why j + 1?// Avoid Repetition: you don’t want to compare it with itself,
                //and you don’t want to count the same 'n' multiple times. 


            {
                if (key == s[i]) {

                    s[i] = '*';//mark for repeated letter
                    count++;// inside each how many time
                }
            }
            if ((key != '*')&&(count>1))// if condition don't print repeated letter
                {
                last=key;
                }
        }System.out.println(last);
    }

}


Enter fullscreen mode Exit fullscreen mode

Output:
a

Most frequent letter in a given String
package afterfeb13;

public class findchar {
public static void main(String[] args) {

    String c = "neelakandan";
    char last=' ';
    char max_char=' ';
    int max =0;
    char[] s=c.toCharArray();// string to char


    for (int j = 0; j < s.length; j++)// selects each character from the array one by one.
    {
        char key = s[j];// s[j] represent all char because j++
        int count = 1;// frist char
        for (int i = j+1; i < s.length; i++) //Why j + 1?// Avoid Repetition: you don’t want to compare it with itself,
            //and you don’t want to count the same 'n' multiple times. 


        {
            if (key == s[i]) {

                s[i] = '*';//mark for repeated letter
                count++;// inside each how many time
            }
        }
        if ((key != '*')&&(count>1))// if condition don't print repeated letter
            {
            if(count>max)
            {
                max=count;// max char
                max_char=key;//max char
            }


            }
    }System.out.println(max +" times "+max_char);
}
Enter fullscreen mode Exit fullscreen mode

}

Output:
3 times n

finding same letter or numb near by near(letter)

package afterfeb13;

public class newpartice {
    public static void main(String[] args) {
        int[] num = { 0, 1, 1, 0 };

        for (int j = 0; j < num.length - 1; j++) {
            if (num[j] == num[j + 1])
            {
                System.out.println(num[j]);
            }
        }
    }

}
Enter fullscreen mode Exit fullscreen mode

output:1

String amma,appa,chennai

package afterfeb13;

public class newpartice {
    public static void main(String[] args) {
        String s = "amma";
        char[] c = s.toCharArray();
        for (int j = 0; j < c.length - 1; j++) {
            if (c[j] == c[j + 1])// c=h//h=e//e=n//n=n//take n==n
            {
                System.out.println(c[j]);
            }
        }
    }

}

Enter fullscreen mode Exit fullscreen mode

Output:m

Top comments (0)