String programs
1) Reverse a given string .
package strings;
import java.util.Scanner;
public class ReverseString {
public static void main(String[] args) {
Scanner sc =new Scanner(System.in);
System.out.println("enter a string");
char[] a=sc.next().toCharArray();
//for reverese an array of char we use a for loop
System.out.print("your String is : ");
for(int i=0;i<a.length;i++)
{
System.out.print(a[i]);
}
System.out.print("\nreverse string is: ");
for(int i=a.length-1;i>=0;i--)
{
System.out.print(a[i]);
}
}
}
2) Reverse string in another way?
package strings;
public class ReverseString2 {
public static void main(String[] args) {
String name="pavap";
String rev="";
System.out.println(name.length());
for(int i=name.length()-1;i>=0;i--)
{
rev=rev+name.charAt(i);
}
System.out.println(rev);
if(name.equals(rev))
{
System.out.println("palindrone");
}else {
System.out.println("not");
}
}
}
3) Convert String as an Array . (by using charToArray() ;)?
package strings;
import java.util.Scanner;
public class StringCharArray {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
System.out.println("enter a string : ");
char[] a =sc.next().toCharArray();
System.out.println(a);
for(int i=0;i<a.length;i++)
{
System.out.println(a[i]);
}
}
}
4) Check given string is palindrome or not ?
package strings;
import java.util.Scanner;
public class StringPalindrone {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("enter your String");
char[] s=sc.next().toCharArray();
int i,j;
int count=0;
for(i=0,j=s.length-1;i<s.length;i++,j--)
{
if(s[i]==s[j])
{
count++;
}
}
if(count>=1)
{
System.out.println("palindrone ");
}
else
{
System.out.println("not palidrone");
}
}
}
1) write java program to convert given string in lowercase
package strings;
public class convertintoLowercase {
public static void main(String[] args) {
String s="RavikirAn";
char[] ch =s.toCharArray();
String nstr=" ";
for(int i=0;i<ch.length;i++)
{
if(ch[i]>=65 && ch[i]<=90)
{
nstr=nstr+(char)(ch[i]+32);
}
else
{
nstr=nstr+ch[i];
}
}
System.out.println(nstr);
}
2. Write a java code to remove space between the string
package strings;
public class RemoveSpaceGivenString {
public static void main(String[] args) {
String s="ravikiran kadam ";
char[] ch =s.toCharArray();
String nstr=" ";
for(int i=0;i<ch.length;i++)
{
if(ch[i] != ' ')
{
nstr=nstr+ch[i];
}
}
System.out.println(nstr);
}
Or
package strings;
public class RemoveSpaceInString {
public static void main(String[] args) {
char a[]="ravikiran kadam well yes all over me
".toCharArray();
for(char i:a)
{
if(i != ' ')
{
System.out.print(i);
}
}
}
3. write a java program to sort string characters into ascending order
package strings;
public class SortAGivenString {
public static void main(String[] args) {
String s="ravi";
char[] ch =s.toCharArray();
for (int i=0;i<ch.length-1;i++)
{
for(int j=i+1;j<ch.length;j++)
{
if(ch[i] > ch[j])
{
char temp =ch[i];
ch[i]=ch[j];
ch[j]=temp;
}
}
}
String name =new String(ch);
System.out.println(name);
}
AnagramProgram
Anagram == given string1 is have all character in
String2 but different in index and no extra
character are present there length of both
string must same.
All the palindrome Strings are anagram.
Example
Str1==Mother in low
Str2==Hitler woman
package strings;
import java.util.Scanner;
public class anagramString {
// remove space b/t strings//
static String removeSpace(String s)
{
char[] ch =s.toCharArray();
String nstr="";
for(int i=0;i<ch.length;i++)
{
if(ch[i]!=' ')
{
nstr=nstr+ch[i];
}
}
return nstr;
}
//convertions of lower case all string characters
static String lowerCase(String s)
{
char[] ch=s.toCharArray();
String nstr="";
for(int i=0;i<ch.length;i++)
{
if(ch[i]>=65 && ch[i]<=90)
{
nstr=nstr+(char)(ch[i]+32);
}
else {
nstr=nstr+ch[i];
}
}
return nstr;
}
//sort a string
static String sort(String s)
{
char[] ch =s.toCharArray();
for(int i=0;i<ch.length;i++)
{
for(int j=i+1;j<ch.length;j++)
{
if(ch[i]>ch[j])
{
char temp=ch[i];
ch[i]=ch[j];
ch[j]=temp;
}
}
}
String s1=new String(ch);
return s1;
}
//compare to strings lengths and whether it is anagram
or not.//
static boolean compare(String s1,String s2)
{
if(s1.length()!=s2.length())
{
return false;
}
else
{
s1=lowerCase(s1);
s2=lowerCase(s2);
s1=sort(s1);
s2=sort(s2);
char[] ch1=s1.toCharArray();
char[] ch2=s2.toCharArray();
for(int i=0;i<ch1.length;i++)
{
if (ch1[i]!=ch2[i])
{
return false;
}
}
}return true;
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("eneter two Strings: ");
String str1=sc.nextLine();
String str2=sc.nextLine();
String s1= removeSpace(str1);
String s2= removeSpace(str2);
boolean b=compare(s1,s2);
if(b)
{
System.out.println("given String are anagram:
");
}
else
{
System.out.println("given String are not
anagram");
}
}
Panagram Program
package strings;
import java.util.Scanner;
public class pangaram {
// remove space b/t strings//
static String removeSpace(String s)
{
char[] ch =s.toCharArray();
String nstr="";
for(int i=0;i<ch.length;i++)
{
if(ch[i]!=' ')
{
nstr=nstr+ch[i];
}
}
return nstr;
}
//check length and panagram or not
static boolean check(String s)
{
int n=26;
if(s.length()<n)
{
return false;
}
for( char i='A';i<'Z';i++)
{
if((s.indexOf(i)<0) && (s.indexOf((char)
(i+32))<0))
{
return false;
}
}
return true;
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("eneter two Strings: ");
String str1=sc.nextLine();
String s1= removeSpace(str1);
boolean b=check(s1);
if(b)
{
System.out.println("a given String is
panagram");
}
else
{
System.out.println("a given String is Not
panagram");
}
}
Write a Program to count number of characters in each WORD ??
package strings;
import java.util.Scanner;
public class CountofcharInEachWord {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter a string");
String str=sc.nextLine();
int count=0;
String nstr="";
for(int i=0;i<str.length();i++)
{
if(str.charAt(i)==' ')
{
nstr=nstr+count;
count=0;
}
else
{
count++;
nstr =nstr+str.charAt(i);
}
}
nstr=nstr+count;
System.out.println("character count "+ nstr);
}
Write a Program to display OCCURENCES of each character in a STRING?
package strings;
import java.util.Scanner;
public class CountNoOfTimeCharOccuerInString {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("enter a string: ");
String s=sc.next();
int n=s.length();
char[] ch=s.toCharArray();
for(int i=0;i<n;i++)
{
int count=1;
for(int j=i+1;j<n;j++)
{
if(ch[i]==ch[j])
{
count++;
int k=j;
while(k<n-1)//remove duplicate element
{
ch[k]=ch[k+1];
k++;
}
n--;
j--;
}
}
System.out.println("a character " +ch[i]+"
occuered for "+count+" times");
}
String nstr="";
for(int i=0;i<n;i++)
{
nstr=nstr+ch[i];
}
System.out.println(nstr);
}
Or
package strings;
import java.util.Scanner;
public class AaaPractice {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("enter a string ");
String s=sc.nextLine();
char[] ch= s.toCharArray();
for(int i=0;i<ch.length;i++)
{
System.out.println(ch[i]+ " ouccered "+occur(ch,ch[i])
+ " times");
}
}
static int occur(char[] ch,char c) {
int count =0;
for(int i=0;i<ch.length;i++ )
{
if(c==ch[i])
{
count++;
}
}
return count;
}
}
input: SharathBhairaraju
output:
character S Occured for 1 times
character h Occured for 3 times
character a Occured for 5 times
character r Occured for 3 times
character t Occured for 1 times
character B Occured for 1 times
character i Occured for 1 times
character j Occured for 1 times
character u Occured for 1 times
ShartBiju
Write a program to display number of LOWERCASE, UPPERCASE, SPECIAL
SYMBOLS, SPACES and DIGITS in a STRING ???
solution-
package strings;
import java.util.Scanner;
public class Count_upper_lower_digit_space_special {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("enter a string: ");
String s=sc.nextLine();
char[] ch=s.toCharArray();
int uc=0,lc=0,spc=0,dc=0,sp=0;
for(int i=0;i<ch.length;i++)
{
if(ch[i]>=65 && ch[i]<=90)
{
uc++;
}
else if(ch[i]>=97 && ch[i]<=122)
{
lc++;
}
else if(ch[i]>=48 && ch[i]<=57)
{
dc++;
}
else if( ch[i]==' ')
{
sp++;
}
else
{
spc++;
}
}
System.out.println("count of uppercase character is : "+
uc);
System.out.println("count of lowercase character is
: "+ lc);
System.out.println("count of digits character is :
"+ dc);
System.out.println("count of white_space character
is : "+ sp);
System.out.println("count of special character is :
"+ spc);
}
output-
enter the string
[email protected]
the count of uppercase 3
the count of lowercase 14
the count of digits 3
the count of space 0
the count of special character 3
Write a program to REVERSE THE WORDS in a SENTENCE?
solution-
package strings;
import java.util.Scanner;
public class Reverse_Words_String {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("enter a string: ");
String s=sc.nextLine();
char[] ch=s.toCharArray();
String rev="";
for(int i=0;i<ch.length;i++)
{
int k=i;
while(i<ch.length && ch[i]!=' ')
{
i++;
}
int j=i-1;
while(k<=j)
{
rev=rev+ch[j];
j--;
}
rev=rev+" ";
}
System.out.println(rev);
input -
Rama and lakshmana
output -
amar dna anamhskal
String Assignment
1. Write a program to REVERSE the SENTENCE?
enter the sentence: rama and lakshmana
The reserved words of sentence is: amar dna anamhskal
2.Write a program to display STRING INITCAP of Words?
enter the string: sundra is a good boy
the first letter of word : Sundra Is A Good Boy
3.Write a program to find a SUB-STRING without using INBUILT functions
enter the main string : SharathBhairaraju
enter the sub string : Bhairaraju
1 times Bhairaraju present