A name is to be said as an Odd name if the ASCII code of each
character becomes an odd number. Write a program to accept a
name and check whether the given name is an odd name or not.
import java.util.*;
class oddName
{
public static void main(String args[])
{
Scanner sc= new Scanner(System.in);
String str = sc.nextLine();
int flag = 0;
for(int i = 0;i<str.length();i++)
{
int asc = (int)str.charAt(i);
if(asc%2 = = 0) flag = 1;
}
if(flag = = 0)
System.out.println (str+" is a odd Name");
else
System.out.println (str+" is not a odd Name");
}
}
Write a program to input a string and print each word of the string in the
reverse order.
Sample Input:
Enter a string: My name is Raman
Sample Output
yM eman si namaR
import java.util.Scanner;
class WordsReverse
{
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a sentence:");
String str = sc.nextLine();
str += " ";
int len = str.length();
String word = "";
int wLen = 0;
char ch;
for (int i = 0; i < len; i++) {
ch = str.charAt(i);
if (ch != ' ') {
word = word + ch;
}
else {
wLen = word.length();
for(int j = wLen - 1; j >= 0; j--) {
System.out.print(word.charAt(j));
}
System.out.print(' ');
word = "";
}
}
}
}
Write a program in Java to accept a string and display the number of
uppercase, number of lowercase, number of special characters and
number of digits present in the string.
import java.util.Scanner;
class KboatCount
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println("Enter a sentence:");
String str = in.nextLine();
int len = str.length();
int uc = 0;
int lc = 0;
int sc = 0;
int dc = 0;
char ch;
for (int i = 0; i < len; i++) {
ch = str.charAt(i);
if (Character.isUpperCase(ch))
uc++;
else if (Character.isLowerCase(ch))
lc++;
else if (Character.isDigit(ch))
dc++;
else if (!Character.isWhitespace(ch))
sc++;
}
System.out.println("UpperCase Count = " + uc);
System.out.println("LowerCase Count = " + lc);
System.out.println("Digit count = " + dc);
System.out.println("Special Character Count = " +
sc);
}
}
Write a program to input a sentence and print each word of the string
along with its length in tabular form.
import java.util.Scanner;
class WordsLengthTable
{
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a sentence:");
String str = sc.nextLine();
str += " ";
int len = str.length();
String word = "";
int wLen = 0;
System.out.println("Word Length");
for (int i = 0; i < len; i++) {
char ch = str.charAt(i);
if (ch != ' ') {
word = word + ch;
wLen++;
}
else {
System.out.println(word + "\t" + wLen);
word = "";
wLen = 0;
}
}
}
}
Write a program in Java to enter a string/sentence and display the
longest word and the length of the longest word present in the string.
Sample Input:
"Tata football academy will play against Mohan Bagan"
Sample Output:
The longest word: Football
The length of the word: 8
import java.util.Scanner;
class LongestWord
{
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a word or sentence:");
String str = sc.nextLine();
str += " ";
String word = "", lWord = "";
int len = str.length();
for (int i = 0; i < len; i++) {
char ch = str.charAt(i);
if (ch == ' ') {
if (word.length() > lWord.length())
lWord = word;
word = "";
}
else {
word += ch;
}
}
System.out.println("The longest word: " + lWord +
": The length of the word: " + lWord.length());
}