String Solved Sums Roshan

Download as pdf or txt
Download as pdf or txt
You are on page 1of 32

STRINGS SOLVED EXAMPLES

ROSHAN GUPTA

PROGRAM 1:
import java.util.*;
class prog1
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter String");
String st=sc.next();
int p=st.length();
for (int i=0;i<p;i++)
{
System.out.println(st.charAt(i));
}
}
}

PROGRAM 2:

import java.util.*;
class prog2
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter String");
String st=sc.nextLine();
int k=0;
for (int i=0;i<st.length();i++)
{
char ch=st.charAt(i);
if
(ch=='a'||ch=='o'||ch=='e'||ch=='u'||ch=='i'||ch=='A'||ch=='E'||ch=='I'||ch=='O'
||ch=='U')
k++;
}
System.out.println("Vowels:"+k);
}
}

PROGRAM 3:

import java.util.*;
class prog3
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter String");
String st=sc.nextLine();
for (int i=0;i<st.length();i++)
{
char ch=st.charAt(i);
if (ch=='e')
System.out.print("*");
else
System.out.print(ch);
}
}
}

PROGRAM 4:

import java.util.*;
class prog4
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter String");
String st=sc.nextLine(),st1="";

for (int i=0;i<st.length();i++)


{
char ch=st.charAt(i),ch1,ch2;
int a=(int)ch;
if (65<=a&&a<=90)
{
ch1=Character.toLowerCase(ch);
st1=st1+ch1;
}

else if (97<=a&&a<=122)
{
ch2=Character.toUpperCase(ch);
st1=st1+ch2;
}

else
st1=st1+ch;

}
System.out.println(st1);
}
}

PROGRAM 5:

import java.util.*;
class prog5
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter String");
String st=sc.nextLine();
int lc=0,uc=0,sp=0,di=0;
for (int i=0;i<st.length();i++)
{
int p=(int)st.charAt(i);
if (p>=65&&p<=90)
uc++;
else if (p>=97&&p<122)
lc++;
else if (p>=48&&p<=57)
di++;
else
sp++;

}
System.out.println("Lower case \n"+lc+"Upper Case \n"+uc+"Digits
\n"+di+"Special Characters \n"+sp);
}
}

PROGRAM 6:

import java.util.*;
class prog6
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter Name");
String name=sc.nextLine();
name=' '+name;
String intitials="";
int p=name.lastIndexOf(' ');
String last=name.substring(p);

for (int i=0;i<p;i++)


{
char ch=name.charAt(i);
if (ch==' ')
{
intitials=intitials+name.charAt(i+1)+'.'+' ';
}

}
System.out.println(intitials+last);

}
}

PROGRAM 7:

import java.util.*;
class prog7
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter Sentence");
String st=sc.nextLine();
st=st+' ';
int p=st.length();
String st1="",st2="";
for (int i=0;i<p;i++)
{
char ch=st.charAt(i);
if (ch==' ')
{
st1=st1+' '+st2;
st2="";
}
else
{
st2=ch+st2;
}
}
System.out.println(st1.trim());
}
}

PROGRAM 8:

import java.util.*;
class prog8
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter Sentence");
String st=sc.nextLine();
System.out.println("Enter Word to be searched");
String ws=sc.next();
String st2="";
st=st+' ';
int k=0;
for (int i=0;i<st.length();i++)
{
char ch=st.charAt(i);
if (ch==' ')
{
if (st2.equalsIgnoreCase(ws)==true)
{
k++;
}
st2="";
}
else
{
st2=st2+ch;
}

}
System.out.println("frequency "+k);
}
}

PROGRAM 9:

import java.util.*;
class prog9
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter Sentence:");
String st=sc.nextLine();
String word="";
for (int i=0;i<st.length();i++)
{
char ch=st.charAt(i);
if
(ch!='a'&&ch!='i'&&ch!='o'&&ch!='u'&&ch!='e'&&ch!='A'&&ch!='E'&&ch!='I'&&c
h!='O'&&ch!='U')
{
char ch1=(char)(((int)ch)-1);
if
(ch1=='E'||ch1=='A'||ch1=='I'||ch1=='O'||ch1=='U'||ch1=='a'||ch1=='e'||ch1=='
i'||ch1=='o'||ch1=='u')
{
char ch2=(char)(((int)ch)+1);
word+=ch2;

}
else
word+=ch1;
}
else
word+=ch;
}
System.out.println(word);
}
}

PROGRAM 10:
DOUBT
PROGRAM 11:

import java.util.*;
class prog11
{
public static void main()
{
int n=0;
Scanner sc=new Scanner(System.in);
System.out.println("Enter String: ");
String st=sc.next();
String re="";
for (int i=(st.length())-1;i>=0;i--)
{
char ch=st.charAt(i);
re=re+ch;

}
if (st.equals(re))
System.out.println("Palindrome");
else
System.out.println("Not palindrome");

}
}

PROGRAM 12:

import java.util.*;
class prog12
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter String");
String st=sc.next();
String st1="",st2="";
for (int i=0;i<st.length();i++)
{
char ch=st.charAt(i);
if
(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u'||ch=='A'||ch=='E'||ch=='I'||ch=='O'
||ch=='U')
{
st1=st.substring(0,i);
st2=st.substring(i);
break;
}
}
System.out.println(st2+st1+"AY");
}
}

PROGRAM 13:

import java.util.*;
class prog13
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter Word");
String st=sc.next();
for (int i=65;i<=90;i++)
{
for (int j=0;j<st.length();j++)
{
char ch=st.charAt(j);
if (ch==(char)i||ch==(char)i+32)
{
System.out.print(ch);
}
}
}
}
}

PROGRAM 14:

import java.util.*;
class prog14
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter sentence");
String st=sc.nextLine();
st=' '+st;
String st1="";
for (int i=0;i<st.length();i++)
{
char ch=st.charAt(i);
if (ch==' ')
{
char ch1=st.charAt(i+1);
st1=st1+' '+Character.toUpperCase(ch1);
i++;
}
else
st1=st1+ch;
}
System.out.println(st1.trim());
}
}

PROGRAM 15:

import java.util.*;
class prog15
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter String");
String st=sc.next();
for (int i=1;i<=st.length();i++)
{
System.out.println(st.substring(0,i));
}
}
}

PROGRAM 16:

import java.util.*;
class prog16
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter String");
String st=sc.next();
int k=0;
for (int i=st.length();i>0;i--)
{
for (int j=0;j<k;j++)
{
System.out.print(' ');
}
System.out.println(st.substring(0,i));
k++;
}
}
}
PROGRAM 17:

import java.util.*;
class prog17
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter String");
String st=sc.next();
System.out.println(st);
for (int i=1;i<st.length()-1;i++)
{
System.out.print(st.charAt(i));
for (int j=0;j<st.length()-2;j++)
System.out.print(' ');
System.out.println(st.charAt(st.length()-1-i));
}
for (int i=st.length()-1;i>=0;i--)
{
System.out.print(st.charAt(i));
}
}
}

PROGRAM 18:

import java.util.*;
class prog18
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter Word");
String st=sc.next();
for (int i=0;i<st.length();i++)
{
System.out.println(st.substring(i,st.length())+st.substring(0,i));
}
}
}

PROGRAM 19:

import java.util.*;
class prog19
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter Three Letter Word:");
String st=sc.next();
for (int i=0;i<3;i++)
{
for (int j=0;j<3;j++)
{
for (int k=0;k<3;k++)
{
if (i!=k&&i!=j&&j!=k)
{
System.out.println(st.charAt(i)+""+st.charAt(j)+""+st.charAt(k));
}
}
}
}
}
}

PROGRAM 20:

import java.util.*;
class prog20
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter 1. or 2.");
switch(sc.nextInt())
{
case 1:
{
System.out.println("Enter number of letters:");
int p=sc.nextInt();
for (int i=69;i>=65;i--)
{
for (int j=65;j<=i;j++)
{
System.out.print((char)(j));
}
System.out.println("");
}
break;
}
case 2:
{
System.out.println("Enter Word");
String st=sc.next();
for (int i=0;i<st.length();i++)
{
for (int j=0;j<=i;j++)
{
System.out.print(st.charAt(i));
}
System.out.println("");
}
}
}
}
}

PROGRAM 21:

import java.util.*;
class prog21
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter Sentence:");
String st=sc.nextLine();
System.out.println("Enter F. for First and L. for Last:");
char ch=sc.next().charAt(0);
switch(ch)
{
case 'F':
{
st=' '+st;
for (int i=0;i<st.length();i++)
{
char ch1=st.charAt(i);
if (ch1==' ')
{
System.out.println(st.charAt(i+1));
}
}
break;
}
case 'L':
{
st=st+' ';
for (int i=0;i<st.length();i++)
{
char ch2=st.charAt(i);
if (ch2==' ')
{
System.out.println(st.charAt(i-1));
}
}
break;
}
default:
System.out.println("Enter Correct Choice");
}
}
}

PROGRAM 22:

import java.util.*;
class prog22
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter Information");
String arrName[]=new String[20];
String arrTele[]=new String[20];
for (int i=0;i<20;i++)
{
System.out.println("Enter Name:");
arrName[i]=sc.next();
System.out.println("Enter Telephone Number:");
arrTele[i]=sc.next();
}
System.out.println("Enter Name to be searched");
String st=sc.next();
int p=0;
for (int i=0;i<20;i++)
{
if (arrName[i].compareTo(st)==0)
{
System.out.println("Name Found"+st);
System.out.println("Telephone"+arrTele[i]);
p++;
break;
}
}
if (p!=0)
System.out.println("Not found");
}
}
PROGRAM 23&24 SORTING:

PROGRAM 25:

import java.util.*;
class prog25
{
public static void main()
{
Scanner sc=new Scanner(System.in);
String arrState[]=new String[10];
String arrCapi[]=new String[10];
for (int i=0;i<10;i++)
{
System.out.println("Enter State");
arrState[i]=sc.next();
System.out.println("Enter Capital");
arrCapi[i]=sc.next();

}
System.out.println("Enter State to be Searched");
String st=sc.next();
int k=0;
for (int i=0;i<arrState.length;i++)
{
if (arrState[i].compareTo(st)==0)
{
System.out.println(arrCapi[i]);
k++;
}
}
if (k!=0)
System.out.println("Sorry not found");
}
}

ARRAY SUMS:

import java.util.*;
class ARRAY1
{
public static void main()
{
Scanner in = new Scanner(System.in);
int no=40;
int english[] = new int[no];
int maths[] = new int[no];
int science[] = new int[no];
double avgMarks[] = new double[no];

for (int i = 0; i <no; i++)


{
System.out.println("Enter student " + (i+1) + " details:");
System.out.print("Marks in English: ");
english[i] = in.nextInt();
System.out.print("Marks in Maths: ");
maths[i] = in.nextInt();
System.out.print("Marks in Science: ");
science[i] = in.nextInt();
avgMarks[i] = (english[i] + maths[i] + science[i]) / 3.0;
}

int engTotal=0, mathsTotal=0, sciTotal=0;

for (int i = 0; i<no; i++)


{
System.out.println("Average marks of student "+(i+1)+" = "+ avgMarks[i]);
engTotal += english[i];
mathsTotal += maths[i];
sciTotal += science[i];
}
System.out.println("Class Average in English = " + ((double)engTotal/no));
System.out.println("Class Average in Maths = " + ((double)mathsTotal/no));
System.out.println("Class Average in Science = " + ((double)sciTotal/no));
}
}

import java.util.*;
class ARRAY2
{
public static void main()
{
Scanner sc=new Scanner(System.in);
double arr[]=new double[10];
System.out.println("Enter 10 numbers:");
for (int i=0;i<10;i++)
arr[i]=sc.nextDouble();
int arr1[]=new int[10];
for (int i=0;i<10;i++)
{
arr1[i]=(int)Math.floor(arr[i]);
}
for (int i=0;i<10;i++)
System.out.println(arr1[i]);
double arr2[]=new double[10];
for (int i=0;i<10;i++)
{
arr2[i]=arr[i]-arr1[i];
}
for (int i=0;i<10;i++)
System.out.println(arr2[i]);

}
}

import java.util.*;
class ARRAY3
{
public static void main(String args[]){
Scanner in=new Scanner(System.in);
System.out.println("Enter numbers");
int a[]=new int[20];
for(int i=0;i<20;i++){
a[i]=in.nextInt();
}
int max=a[0];
int min=a[0];
int sum=0;
for(int i=0;i<20;i++){
if(a[i]>max){
max=a[i];
}
else if(a[i]<min){
min=a[i];
}
sum=sum+a[i];
}
System.out.println("Largest number is"+max);
System.out.println("Smallest number is"+min);
System.out.println("sum is"+sum);
}
}

import java.util.*;
Class ARRAY4
{
public static void main()
{
Scanner sc=new Scanner(System.in);
int p[]=new int[6];
int q[]=new int[4];
int r[]=new int[10];
System.out.println("Enter elements in Array P");
for (int i=0;i<6;i++)
p[i]=sc.nextInt();
System.out.println("Enter elements in Array Q");
for (int i=0;i<4;i++)
q[i]=sc.nextInt();
for (int i=0;i<6;i++)
r[i]=p[i];
for (int i=0;i<4;i++)
r[i+6]=q[i];
for (int i=0;i<10;i++)
System.out.println(r[i]);
}
}

You might also like