Let's learn java programming language with easy steps. This Java tutorial provides you complete knowledge about java technology.

Saturday 24 July 2021

Write a Java Program To Count Letters in String

How To Count Letters in String in Java


In the last post, we have discussed how to find character or character position in string in java with simple examples and now here we are going to create a simple example for how to count letters in java string with examples.

So let's start with a useful example for finding the number of letters in a given java String.

The below example gives you a total number of letters count in a given java string i.e (given string is "sun45tt" and letters in this give string is suntt i.e 5).

Java Program to Count Letters in String

class LettersCountInString
{
public static void main(String args[])
{
String str = "sun45tt";
int count = 0; 
for(int i = 0; i<str.length(); i++)
{
if(Character.isLetter(str.charAt(i)))
{
count++;
}
}
System.out.println(count);
}
}

Output: 5

Here we discussed, how to write a java program to count letters in string with a simple example.


Share:

Friday 23 July 2021

How Do I Find a Character in a String in Java

 Searching Characters in a String in Java

In the last article, we have learned how do I find a first non-repeated character in string in java and now here we are going to make a java program for how do I find a character in a string with a simple example.

Here we will find the index position of a character given in a java string by using the pre-defined method of a java String Object.

Here we will find the character position in a sting by using the indexOf( ) method of the String class.

So let's start searching for specific character in string.

Find Character in Java String

class FindingCharacterInString
{
public static void main(String args[])
{
String str = "how are you men";
int indexPosition = str.indexOf('a'); //finding character a String
System.out.println("Index Position of character a is  "+indexPosition);
}
}

Output : Index Position of character is 4

Note the above program for finding a character will give you the first occurrence of the character's position not the second occurrence of a character.

Let's understand by simple example where we again find the character in a string but with a different character.

class FindingCharacterInString
{
public static void main(String args[])
{
String str = "how are you men";
int indexPosition = str.indexOf('o'); //finding character String
System.out.println("Index Position of character is  "+indexPosition);
}
}

Output: Index Position of character is 1

So here we discussed some useful examples for how do i find a character in a String in java.
Share:

Monday 19 July 2021

How Do You Find The First Non Repeated Character of a String in Java

Find First Non Repeating Character in a String in Java 

Now Here we are going print First non repeated character in String in java through a simple example or java program so that you can understand well.

So let's start a simple java program for find the first non repeated character of a String.

For example, we take a string in  java like String str = "akalsys";

So in above string there is k is a first non repeated character in String.

Java Example for First Non-Repeated Character in String.

class FirstNonRepeatedCharacterInString
{
public static void main(String args[])
{
String str = "akalsys";
for(char ch : str.toCharArray( ))
{
if(str.indexOf(ch) == str.lastIndexOf(ch))
{
System.out.println(ch);
break;
}
}
}
}

Output : k

Above example will you give the k as output.

Here we learned, how to find the first non repeated character in java string with a simple example.

Share:

Facebook Page Likes

Follow javatutorial95 on twitter

Popular Posts

Translate