Skip to content

Commit

Permalink
Count words in a string whose 1st and last letter same
Browse files Browse the repository at this point in the history
  • Loading branch information
Arijit-SE committed Apr 25, 2023
1 parent 20020f1 commit d79af4e
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions Same_1st_2nd_Letter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*Count the number of words in a sentence that start and end with the same letter.
Input: Anna asked about the Ginseng recipe
Output: 2
Explanation: There are two words in the sentence that start and end with the same letter - "Anna" and "Ginseng". */

import java.util.*;
public class Same_1st_2nd_Letter {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s = sc.nextLine().toLowerCase();
String[] str = s.split(" ");
int count=0;
for (int i = 0; i < str.length; i++) {
if(str[i].charAt(0)==str[i].charAt(str[i].length()-1))
{
count++;
}
}
if(count>0)
{
System.out.println(count);
}
else
{
System.out.println("No word found");
}
}
}

0 comments on commit d79af4e

Please sign in to comment.