-
Notifications
You must be signed in to change notification settings - Fork 109
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Count words in a string whose 1st and last letter same
- Loading branch information
Showing
1 changed file
with
30 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} | ||
} |