26.8k views
1 vote
Write a program which takes a String input, and then prints the number of times the string "sh" appears in the String (s and h can be any case). Hint - remember there are String methods which return a copy of the String it is called on with all letter characters changed to the same case.

User Nate Fox
by
5.3k points

2 Answers

4 votes

Answer:

Scanner scan=new Scanner(System.in);

System.out.println("Input String:");

String str = scan.nextLine();

int count = 0;

str=str.toLowerCase();

for (int i = 0; i < str.length()-1; i++)

{

if (str.substring(i, i + 2).equals("sh"))

{

count++;

}

}

System.out.println("Contains \"sh\" " + count + " times.");

}

}

Step-by-step explanation:

This is what I was able to get for Java.

User Shayonj
by
5.5k points
4 votes

Answer:

str = input("Input: ")

str = str.lower()

counts = str.count("sh")

print(counts)

Step-by-step explanation:

I answered the question in Python programming language.

[This line prompts user for input]

str = input("Input: ")

[This line converts user input to lower case]

str = str.lower()

[This counts the occurrence of "sh" in the input string]

counts = str.count("sh")

[This prints the number of occurrence of "sh" in the user input]

print(counts)

User Kwogger
by
4.6k points