0% found this document useful (0 votes)
12 views13 pages

Java String Manipulation Methods

The document contains a series of Java programming exercises that involve string manipulation, including methods to convert case, replace characters, concatenate strings, count characters, and analyze string properties. Each exercise is presented with a class definition and method implementation, demonstrating various string operations such as counting vowels, digits, and special characters, as well as reversing strings and printing characters with their ASCII values. The exercises are aimed at practicing Java programming skills related to string handling.

Uploaded by

dushyant parihar
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)
12 views13 pages

Java String Manipulation Methods

The document contains a series of Java programming exercises that involve string manipulation, including methods to convert case, replace characters, concatenate strings, count characters, and analyze string properties. Each exercise is presented with a class definition and method implementation, demonstrating various string operations such as counting vowels, digits, and special characters, as well as reversing strings and printing characters with their ASCII values. The exercises are aimed at practicing Java programming skills related to string handling.

Uploaded by

dushyant parihar
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

Q.

1: WAP a java method convertCase(String obj1,String obj2) that converts obj1


to
lowercase and obj2 to uppercase. Display the result.
class s1
{
public static void convertCase(String obj1,String obj2)
{
String obj3="",obj4="";
obj3=[Link]();
obj4=[Link]();
[Link]("String in lower case = "+obj3);
[Link]("String in Upper case = "+obj4);
}
}
Q.2: WAP a java program to read string from console and replace all occurrences
of the sixth character of the string by the digit 9.
import [Link].*;
class s2
{
public static void main()
{
Scanner sc=new Scanner([Link]);
String s;
char ch;
[Link]("Enter any string");
s=[Link]();
ch=[Link](5); }
s=[Link](ch,'9'); }
[Link]("Chan
ged string ="+s);
Q.3: WAP to concatenate two string str1 and str2 entered from the
keyboard.
Define a user defined method join to perform the above task.
class s3
{
public static void main(String str1,String str2)
{
String str3="",str4="";
str3=[Link](" ");
str4=[Link](str2);
[Link]("Merged String = "+str4);
}
}
Q.4: WAP to input any string calculate and prints its each character in
different line
with their ASCII code.
class s4
{
public static void main(String s)//CAT
{
int len;
char ch;
len=[Link]();
for(int i=0;i<len-1;i++)
{
ch=[Link](i);
[Link](ch+"\t"+(int)ch);
}
}
}
Q.5: (a) Define a method that finds the number of digits of a number
entered by
the user from the keyboard. Show use of string functions.
class s5a
{
public static void main(String s)
{
int len,DG=0;
char ch;
len=[Link]();
for(int i=0;i<len;i++)
{
ch=[Link](i);//'A'//'8'//'7'/B//'$'
if([Link](ch))
DG++;//2
}
[Link]("No. of digits ="+DG);
}
}
(b) Define another method in the class that replaces the first digit to 0.
class s5b
{
public static void main(String s)
{
char ch=[Link](0);
s=[Link](ch,'0');
[Link]("Updated String = "+s);
}
}
Q.6: WAP to input any string count and print no of Uppercase letters,
Lowercase
letter and no of digits in it.
import [Link].*;
class s6
{
public static void main()
{
Scanner SC=new Scanner([Link]);
String s;
[Link]("Enter any string");
s=[Link]();
int len,UC=0,LC=0,DG=0;
char ch;
len=[Link]();
for(int i=0;i<len;i++)
{
ch=[Link](i);
if([Link](ch))
UC++;//1
else if([Link](ch))
LC++;//2
else if([Link](ch))
DG++;//2
}
[Link]("No of UpperCase = "+UC);
[Link]("No of LowerCase = "+LC);
[Link]("No of Digits = "+DG);
}
}
Q.7: WAP to input any string count and print no of vowels and constant
in it.
class p7
{
public static void main(String s)
{
int len,vw=0,cs=0;
char ch;
len=[Link]();
s=[Link]();
for(int i=0;i<len;i++)
{
ch=[Link](i);
if(ch=='A'||ch=='E'||ch=='I'||c
h=='O'||ch=='U')
vw++;//1
else if(ch>='A' && ch<='Z')
cs++;//2
}
[Link]("No of
vowels = "+vw);
[Link]("No of
Consonant = "+cs);
}
}
Q.8: WAP to input any
String if its first and last char
are same then print the Sting
in reverse order.
class s8
{
public static void main(String s)
{
int len;
char ch1,ch2,ch;
String Rev="";
len=[Link]();
ch1=[Link](0);
ch2=[Link](len-1);
if(ch1==ch2)
{
for(int i=len-1;i>=0;i--)
{
ch=[Link](i);
Rev=Rev+ch;
}
[Link]("Reverse of string = "+Rev);
}
else
[Link]("First and last character are not same");
}
}
Q.9: WAP to input any string count & print no of words in it.
class s9
{
public static void main(String s)
{
int len,sp=0;
char ch;
len=[Link]();
for(int i=0;i<len;i++)
{
ch=[Link](i);
if(ch==' ')
sp++;
}
[Link]("No of words = "+(sp+1));
}
}
Q.10: WAP to input a string and print the string in reverse order.
class s10
{
public static void main(String s)
{
String Rev="";
int len;
char ch;
len=[Link]();
for(int i=len-1;i>=0;i--)
{
ch=[Link](i);
Rev=Rev+ch;//TAC
}
[Link]("reverse of string = "+Rev);
}
}
Q.11: WAP to input any string and print it each word in different line.
class s11
{
public static void main(String s)
{
int len;
char ch;
String Word="";
s=[Link]();
s=s+" ";
len=[Link]();
for(int i=0;i<len;i++)
{
ch=[Link](i);
if(ch!=' ')
Word=Word+ch;//GREAT
else
{
[Link](Word);
Word="";
}
}
}
}
Q.12: WAP to input any string count and print no of special character
in it.
class s12
{
public static void main(String s)
{
int len,sc=0;
char ch;
len=[Link]();
for(int i=0;i<len;i++)
{
ch=[Link](i);
if([Link](ch)
==false)
sc++;//2
}
[Link]("No. of
special characters = "+sc);
}
}
Q.13: WAP to input any string
count and print no of letters ,no
of digits, no of
words and no of special
character in it.
class s13
{
public static void main(String s)
{
int len,Lt=0,Dg=0,sp=0,spc=0;
char ch;
len=[Link]();
for(int i=0;i<len;i++)
{
ch=[Link](i);
if([Link](ch))
Lt++;
else if([Link](ch))
Dg++;
else if(ch==' ')
sp++;
else
spc++;
}
[Link]("No of Letters = "+Lt);
[Link]("No of Digits = "+Dg);
[Link]("No of words = "+(sp+1));
[Link]("No of Special Character = "+spc);
}
}
Q.14: WAP to input any String if it starts with vowel then count and
print no of
letter otherwise count and print no of digits.
class s14
{
public static void main(String s)
{
int len,Lt=0,Dg=0;
char ch,ch1;
len=[Link]();
ch1=[Link](0);
if(ch1=='A'||ch1=='E'||ch1=='I'||ch1=='O'||ch1=='U'||ch1=='a'||ch1=='e'||c
h1=='i'||ch1=='o'||ch1=='u')
{
for(int i=0;i<len;i++)
{
ch=[Link](i);
if([Link](ch))
Lt++;
}
[Link]("No of
Letters = "+Lt);
}
else
{
for(int j=0;j<len;j++)
{
ch=[Link](j);
if([Link](ch))
Dg++;
}
[Link]("No of
Digits = "+Dg);
}
}
}
Q.15: WAP to input any
String if its first character
is vowel then print the
string in
reverse order otherwise
count and print no of
vowels in it.
class s15
{
public static void main(String s)
{
int len,vw=0;
char ch,ch1;
String Rev="";
s=[Link]();
len=[Link]();
ch1=[Link](0);
if(ch1=='A'||ch1=='E'||ch1=='I'||ch1=='O'||ch1=='U')
{
for(int i=len-1;i>=0;i--)
{
ch=[Link](i);
Rev=Rev+ch;//TCA
}
[Link]("Reverse of String = "+Rev);
}
else
{
for(int j=0;j<len;j++)
{
ch=[Link](j);
if(ch1=='A'||ch1=='E'||ch1=='I'||ch1=='O'||ch1=='U')
vw++;
}
[Link]("No of vowels = "+vw);
}
}
}

Common questions

Powered by AI

The program uses a for-loop to iterate through the string, utilizing 'Character.isUpperCase()', 'Character.isLowerCase()', and 'Character.isDigit()' to classify each character, incrementing respective counters. Such classification is crucial for tasks needing validation and formatting, like case-sensitive credentials or numeric-only input fields .

The program iterates over each character of the string, checking if it is a digit using the 'Character.isDigit()' method. It increments a counter for each digit found in the string, effectively counting the total number of digits present .

The program counts spaces in the string, using this count as the baseline for word separation. By iterating through the string and incrementing a space counter, it calculates the number of words as 'space count + 1'. While generally reliable, this method assumes well-formed input with words properly separated by spaces, which might not handle consecutive spaces accurately .

The 'convertCase' method takes two string inputs. For the first string, 'obj1', it converts all characters to lowercase using the 'toLowerCase()' method. For the second string, 'obj2', it converts all characters to uppercase using the 'toUpperCase()' method. The results display the lowercase version of 'obj1' and the uppercase version of 'obj2' .

The logic involves locating the first digit of the string by using 'charAt(0)' and replacing it with '0' using the 'replace' method. This could be useful for scenarios like standardizing formats where only the first numeric occurrence needs alteration, such as product codes or identifiers that conform to a specific template .

The method described reads a string from the console and targets the sixth character (index 5, considering zero-based indexing) of the string. This character is identified using 'charAt(5)' and is replaced with the digit '9' using the 'replace' method. The choice of the sixth character seems arbitrary and is likely meant to demonstrate the replace functionality for a specific character index .

The method converts the string to uppercase for uniform comparison, then iterates through each character, checking for vowels using 'if' conditions (A, E, I, O, U). Characters not matching vowels but within 'A' to 'Z' are counted as consonants. This approach efficiently counts both vowels and consonants, leveraging simple checks for uppercase letters .

The method calculates the length of the input string and iterates through it with a for-loop. For each character at index 'i', the character is printed along with its ASCII integer value, which is obtained by casting the character to an 'int', using '(int)ch' .

The program first compares the first and last characters of the string using 'charAt(0)' and 'charAt(len-1)'. If they match, it proceeds to reverse the string using a for-loop that starts from the end and appends each character to a new string, 'Rev'. If the characters do not match, it outputs a message stating the mismatch .

The program concatenates two strings entered by the user through a user-defined method 'join'. It first appends a space to the first string using 'concat(" ")' and then concatenates the second string to it, resulting in a single merged string output .

You might also like