0% found this document useful (0 votes)
4 views3 pages

Java String Programs Q11 To Q20 ICSE

The document contains Java code snippets for various string manipulation tasks, including counting spaces and words, counting vowels and consonants, separating vowel and consonant words, printing each word on a new line, counting occurrences of 'yes', replacing words, reversing strings, sorting characters, and swapping letters. Each task is encapsulated in a separate class with a main method for execution. The code demonstrates basic string operations and user input handling in Java.

Uploaded by

Soumya Ghosal
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)
4 views3 pages

Java String Programs Q11 To Q20 ICSE

The document contains Java code snippets for various string manipulation tasks, including counting spaces and words, counting vowels and consonants, separating vowel and consonant words, printing each word on a new line, counting occurrences of 'yes', replacing words, reversing strings, sorting characters, and swapping letters. Each task is encapsulated in a separate class with a main method for execution. The code demonstrates basic string operations and user input handling in Java.

Uploaded by

Soumya Ghosal
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/ 3

11.

Count total spaces and words

import java.util.*;
class Q11 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
int sp = 0, wd = 1;
for (int i = 0; i < s.length(); i++)
if (s.charAt(i) == ' ') sp++;
wd = sp + 1;
System.out.println("Spaces: " + sp);
System.out.println("Words: " + wd);
}
}

12. Count vowels and consonants

import java.util.*;
class Q12 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s = sc.nextLine().toLowerCase();
int v = 0, c = 0;
for (int i = 0; i < s.length(); i++) {
char ch = s.charAt(i);
if ("aeiou".indexOf(ch) >= 0) v++;
else if (ch >= 'a' && ch <= 'z') c++;
}
System.out.println("Vowels: " + v);
System.out.println("Consonants: " + c);
}
}

13. Separate vowel and consonant words

import java.util.*;
class Q13 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s = sc.nextLine().toUpperCase();
String[] w = s.split(" ");
String vw = "", cw = "";
for (String x : w) {
char ch = x.charAt(0);
if ("AEIOU".indexOf(ch) >= 0) vw += x;
else cw += x;
}
System.out.println("Vowel word = " + vw);
System.out.println("Consonant word = " + cw);
}
}

14. Print each word in separate line

import java.util.*;
class Q14 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String[] w = sc.nextLine().split(" ");
for (String x : w)
System.out.println(x);
}
}

15. Count occurrences of "yes" or "YES"

import java.util.*;
class Q15 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String[] w = sc.nextLine().split(" ");
int c = 0;
for (String x : w)
if (x.equalsIgnoreCase("yes")) c++;
System.out.println(c);
}
}

16. Define method to count word occurrences

class Q16 {
static int FindWords(String s, String w) {
String[] a = s.split(" ");
int c = 0;
for (String x : a)
if (x.equalsIgnoreCase(w)) c++;
return c;
}

public static void main(String[] args) {


System.out.println(FindWords("Yes no YES yes", "yes"));
}
}

17. Replace "are" by "was"

import java.util.*;
class Q17 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
String r = s.replace("are", "was");
System.out.println("Old: " + s);
System.out.println("New: " + r);
}
}

18. Reverse string and check if same

import java.util.*;
class Q18 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s1 = sc.nextLine();
String s2 = new StringBuilder(s1).reverse().toString();
System.out.println("Rev: " + s2);
System.out.println(s1.equals(s2) ? "Same" : "Not Same");
}
}

19. Uppercase, sort, print

import java.util.*;
class Q19 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s = sc.nextLine().toUpperCase();
char[] ch = s.toCharArray();
Arrays.sort(ch);
String s2 = new String(ch);
System.out.println("Orig: " + s);
System.out.println("Sort: " + s2);
}
}

20. Swap first and last letter

import java.util.*;
class Q20 {
static void Change(String s) {
if (s.length() < 2) {
System.out.println("Orig: " + s);
System.out.println("New: " + s);
return;
}
char[] a = s.toCharArray();
char t = a[0];
a[0] = a[a.length - 1];
a[a.length - 1] = t;
String n = new String(a);
System.out.println("Orig: " + s);
System.out.println("New: " + n);
}

public static void main(String[] args) {


Change("Warm");
}
}

You might also like