Sie sind auf Seite 1von 3

Date:11/07/2016

Worksheet Strings (Grade 10)

1. Write a program to input a word from the user and remove the duplicate characters present in it.
INPUT Mississippi
OUTPUT Misp

2. Write a program to find the shortest and the longest word in a sentence and print them along with their length.
Sample Input: I am learning Java
Sample Output:
Shortest word = I
Length = 1
Longest word = learning
Length = 8
3. Write a program in Java to accept the name and weight of 20 of your friends. Store the weighting a double type array
weight [ ] and the name in a String type array name [ ]. Then sort the name of your friends in
ascending order of their weights and finally print the result.
4. Write Java statements to:
(i) replace all occurrences of character r with h in the String Programmer
(ii) initialize a character array ch with the characters of the word Transition
(iii) convert a character stored in variable test into Upper Case
5. Consider the following string:
String hannah = "Did Hannah see bees? Hannah did.";
a.

What is the value displayed by the expression hannah.length()?

b.

What is the value returned by the method call hannah.charAt(12)?

c.

Write an expression that refers to the letter b in the string referred to by hannah.

6. How long is the string returned by the following expression? What is the string?
"Was it a car or a cat I saw?".substring(9, 12)

Date:11/07/2016

Worksheet Strings (Grade 10)

1. Write a program to input a word from the user and remove the duplicate characters present in it.
INPUT Mississippi
OUTPUT Misp

2. Write a program to find the shortest and the longest word in a sentence and print them along with their length.
Sample Input: I am learning Java
Sample Output:
Shortest word = I
Length = 1
Longest word = learning
Length = 8
3. Write a program in Java to accept the name and weight of 20 of your friends. Store the weighting a double type array
weight [ ] and the name in a String type array name [ ]. Then sort the name of your friends in ascending order
of their weights and finally print the result.

4. Write Java statements to:


(i) replace all occurrences of character r with h in the String Programmer
(ii) initialize a character array ch with the characters of the word Transition
(iii) convert a character stored in variable test into Upper Case
5. Consider the following string:
String hannah = "Did Hannah see bees? Hannah did.";
a.

What is the value displayed by the expression hannah.length()?

b.

What is the value returned by the method call hannah.charAt(12)?

c.

Write an expression that refers to the letter b in the string referred to by hannah.

6. How long is the string returned by the following expression? What is the string?
"Was it a car or a cat I saw?".substring(9, 12)

1.
import java.io.*;
class RemoveDupChar
{
void main()
{
Scanner obj=new Scanner(System.in);
System.out.print("Enter any word : ");
String s = obj.nextLine();
int l = s.length();
char ch;
String ans="";
for(int i=0; i<l; i++)
{
ch = s.charAt(i);
if(ch!=' ')
ans = ans + ch;
s = s.replace(ch,' ');

2. import java.io.*;
class Short_long_word
{
void main()
{
Scanner obj=new Scanner(System.in));
System.out.print("Enter any sentence : "); //inputting the sentence
String s=obj.nextLine();
s=s+" "; //adding a space at the end, to extract the last word also
int len=s.length(); //finding the length of the sentence
String x="",maxw="",minw="";
char ch;
int p,maxl=0,minl=len;
for(int i=0;i<len;i++)
{
ch=s.charAt(i); //extracting characters of the string one at a
time
if(ch!=' ')
{
x=x+ch; //adding characters to form word if character is not

//Replacing all occurrence of the current character by a


space
}
System.out.println("Word after removing duplicate characters : " +
ans);
}
}

space
}
else
{
p=x.length();
if(p<minl) //checking for minimum length
{
minl=p;
minw=x;
}
if(p>maxl) //checking for maximum length
{
maxl=p;
maxw=x;
}
x=""; //emptying the temporary variable to store next word
}
}
System.out.println("Shortest word = "+minw+"\nLength = "+minl);
System.out.println("Longest word = "+maxw+"\nLength =
"+maxl);
}
}

Das könnte Ihnen auch gefallen