0% found this document useful (0 votes)
25 views2 pages

Case Study 4

The document contains two Java code snippets. The first splits a string into an ArrayList of substrings separated by spaces and prints the substrings in reverse order. The second counts the number of occurrences of a given character in a string and prints the count.

Uploaded by

FrancisPlays
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views2 pages

Case Study 4

The document contains two Java code snippets. The first splits a string into an ArrayList of substrings separated by spaces and prints the substrings in reverse order. The second counts the number of occurrences of a given character in a string and prints the count.

Uploaded by

FrancisPlays
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

1.

package Final;
import java.util.*;

public class Main {

public static void main(String[] args) {


Scanner x = new Scanner(System.in);
ArrayList<String> s = new ArrayList();
System.out.print("Input a string: ");
String string = x.nextLine();
String holder = "";
for (int i = 0; i < string.length(); i++)
{
if (string.charAt(i) == ' ' || i == string.length() - 1)
{
if (i == string.length() - 1)
{
holder += string.charAt(i);
}
s.add(holder + " ");
holder = "";
}
else
{
holder += string.charAt(i);
}

}
for (int i = s.size(); i > 0; i--)
{
System.out.print(s.get(i - 1));
}
}

}
2. package Final;
import java.util.*;

public class Main {

public static void main(String[] args) {


Scanner x = new Scanner(System.in);
System.out.print("Enter a word and a character: ");
String string = x.next();
char c = x.next().charAt(0);
int count = 0;

for (int i = 0; i < string.length(); i++)


{
if (string.charAt(i) == c)
{
count++;
}
}
System.out.printf("The letter '" + c + "' occurs " + count + " time/s in the
word \"%s\"\n", string);
}

You might also like