STRING PROGRAMS
Program no:1
// to find the length of a string
import [Link].*;
public class slength
{
public void main()
{
Scanner sc = new Scanner([Link]);
[Link]("Enter your String.");
String k = [Link]();
int a = [Link]();
[Link]("The length of String "+k+" = "+a);
}
}
Program no:2
//CONCATENATION OF TWO STRINGS
import [Link].*;
public class Sconcatenate
{
public void main()
{
Scanner sc = new Scanner([Link]);
[Link]("Enter Two Strings.");
String a = [Link]();
a=a+" ";
String b = [Link]();
String c = [Link](b);
[Link](c);
}
}
Program no: 3
//To reverse a word
import [Link].*;
public class wordreverse
{
public void main()
{
Scanner sc = new Scanner([Link]);
String b="";
[Link]("Enter the word");
String a = [Link]();
int l = [Link]();
for(int i=l-1;i>=0;i--)
b=b+[Link](i);
[Link]("Reversed word="+b);
}
}
Program no: 4
//pgm to check palindrome or not
import [Link].*;
public class wordpali
{
public void main()
{
Scanner sc = new Scanner([Link]);
String b="";
[Link]("Enter the word");
String a = [Link]();
int l = [Link]();
for(int i=l-1;i>=0;i--)
b=b+[Link](i);
if([Link](b))
[Link](a+ "is palindrome");
else
[Link](a+" is not palindrome");
}
}
Program no :5
//to find the no: of different types of characters/digits in a string
import [Link].*;
public class types_of_char
{
public void main()
{
Scanner sc = new Scanner([Link]);
[Link]("Enter the String");
String a = [Link]();
int l = [Link](),uc=0,lc=0,d=0,lt=0,ld=0,ws=0;
for(int i=0;i<l;i++)
{
char b = [Link](i);
if([Link](b))
uc++;
if([Link](b))
lc++;
if([Link](b))
d++;
if([Link](b))
lt++;
if([Link](b))
ld++;
if([Link](b))
ws++;
}
[Link]("No: of Uppercase letters = "+uc);
[Link]("No: of Lowercase letters = "+lc);
[Link]("No: of digits = "+d);
[Link]("No: of letters = "+lt);
[Link]("No: of Letters and Digits = "+ld);
[Link]("No: of Whitespace "+ws);
}
}
Program no :6
// To replace the last Y with “ies”
import [Link].*;
public class End_Not_y
{
public void main()
{
Scanner sc = new Scanner([Link]);
[Link]("Enter the String.");
String a = [Link]();
int l = [Link]();
if([Link](l-1)=='y'||[Link](l-1)=='Y')
a=[Link](0,l-1)+"ies";
[Link]("Modified String = "+a);
}
}
Program no:7
// to find how many times a character repeats in a string
import [Link].*;
public class charfinder
{
public void main()
{
Scanner sc = new Scanner([Link]);
[Link]("Enter the string.");
String a = [Link]();
[Link]("Enter the Character.");
char b = ([Link]()).charAt(0);
int l = [Link](),flag=0;
for(int i=0;i<l;i++)
{
char c= [Link](i);
if(b==c)
{
flag++;
}
}
[Link]("The no: of times "+b+" has repeated "+flag);
}
}
Program no: 8
//to convert uppercase letters to lower case and vice-versa
import [Link].*;
public class Uc_to_Lc_and_vc
{
public void main()
{
Scanner sc = new Scanner([Link]);
[Link]("Enter the String.");
String a = [Link](), b="";
int l = [Link]();
for(int i=0;i<l;i++)
{
char c = [Link](i);
if([Link](c))
b+=[Link](c);
else if([Link](c))
b+=[Link](c);
else
b+=c;
}
[Link]("Original String = "+a);
[Link]("Modified String = "+b);
}
}
Program no:9
//To find the no: of vowels in the String
import [Link].*;
public class vowel_checker
{
public void main()
{
Scanner sc = new Scanner([Link]);
char a[]={'A','E','I','O','U'};
[Link]("Enter the String");
String b = [Link]();
b=[Link]();
int lb = [Link](),la=[Link],flag=0;
for(int i=0;i<lb;i++)
{
char m =[Link](i);
for(int j=0;j<la;j++)
if(m==a[j])
flag++;
}
[Link]("The no: of vowels "+ flag);
}
}
OR
Another method-The no of vowels in a string
import [Link].*;
public class vowel_checker2
{
public void main()
{
Scanner sc = new Scanner([Link]);
[Link]("Enter the String");
String b = [Link]();
b=[Link]();
int lb = [Link](),flag=0;
for(int i=0;i<lb;i++)
{
char m =[Link](i);
if(m=='A'||m=='E'||m=='I'|| m=='O'||m=='U')
flag++;
}
[Link]("The no: of vowels "+ flag);
}
}
Program no:10
//To sort a word Lexicographically(order of the dictionary)
import [Link].*;
public class Lex_Sort
public void main()
Scanner sc = new Scanner([Link]);
[Link]("Enter the Word.");
String a = [Link](),n="";
int i,l=[Link]();
char j,k;
for(j='A',k='a';j<='Z';j++,k++)
for(i=0;i<l;i++)
if([Link](i)==j||[Link](i)==k)
n+=[Link](i);
[Link]("Original Word = "+a);
[Link]("New Word = "+n);