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

String Solved Sums Roshan

This document contains 20 Java programs that demonstrate various string manipulation techniques in Java, such as printing individual characters, counting vowels, replacing characters, capitalizing first letters of words, checking palindromes, substring extraction and more. Each program is presented with its full code and sample input/output.

Uploaded by

Roshan Gupta
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 views32 pages

String Solved Sums Roshan

This document contains 20 Java programs that demonstrate various string manipulation techniques in Java, such as printing individual characters, counting vowels, replacing characters, capitalizing first letters of words, checking palindromes, substring extraction and more. Each program is presented with its full code and sample input/output.

Uploaded by

Roshan Gupta
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

STRINGS SOLVED EXAMPLES

ROSHAN GUPTA

PROGRAM 1:
import [Link].*;
class prog1
{
public static void main()
{
Scanner sc=new Scanner([Link]);
[Link]("Enter String");
String st=[Link]();
int p=[Link]();
for (int i=0;i<p;i++)
{
[Link]([Link](i));
}
}
}

PROGRAM 2:

import [Link].*;
class prog2
{
public static void main()
{
Scanner sc=new Scanner([Link]);
[Link]("Enter String");
String st=[Link]();
int k=0;
for (int i=0;i<[Link]();i++)
{
char ch=[Link](i);
if
(ch=='a'||ch=='o'||ch=='e'||ch=='u'||ch=='i'||ch=='A'||ch=='E'||ch=='I'||ch=='O'
||ch=='U')
k++;
}
[Link]("Vowels:"+k);
}
}

PROGRAM 3:

import [Link].*;
class prog3
{
public static void main()
{
Scanner sc=new Scanner([Link]);
[Link]("Enter String");
String st=[Link]();
for (int i=0;i<[Link]();i++)
{
char ch=[Link](i);
if (ch=='e')
[Link]("*");
else
[Link](ch);
}
}
}

PROGRAM 4:

import [Link].*;
class prog4
{
public static void main()
{
Scanner sc=new Scanner([Link]);
[Link]("Enter String");
String st=[Link](),st1="";

for (int i=0;i<[Link]();i++)


{
char ch=[Link](i),ch1,ch2;
int a=(int)ch;
if (65<=a&&a<=90)
{
ch1=[Link](ch);
st1=st1+ch1;
}

else if (97<=a&&a<=122)
{
ch2=[Link](ch);
st1=st1+ch2;
}

else
st1=st1+ch;

}
[Link](st1);
}
}

PROGRAM 5:

import [Link].*;
class prog5
{
public static void main()
{
Scanner sc=new Scanner([Link]);
[Link]("Enter String");
String st=[Link]();
int lc=0,uc=0,sp=0,di=0;
for (int i=0;i<[Link]();i++)
{
int p=(int)[Link](i);
if (p>=65&&p<=90)
uc++;
else if (p>=97&&p<122)
lc++;
else if (p>=48&&p<=57)
di++;
else
sp++;

}
[Link]("Lower case \n"+lc+"Upper Case \n"+uc+"Digits
\n"+di+"Special Characters \n"+sp);
}
}

PROGRAM 6:

import [Link].*;
class prog6
{
public static void main()
{
Scanner sc=new Scanner([Link]);
[Link]("Enter Name");
String name=[Link]();
name=' '+name;
String intitials="";
int p=[Link](' ');
String last=[Link](p);

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


{
char ch=[Link](i);
if (ch==' ')
{
intitials=intitials+[Link](i+1)+'.'+' ';
}

}
[Link](intitials+last);

}
}

PROGRAM 7:

import [Link].*;
class prog7
{
public static void main()
{
Scanner sc=new Scanner([Link]);
[Link]("Enter Sentence");
String st=[Link]();
st=st+' ';
int p=[Link]();
String st1="",st2="";
for (int i=0;i<p;i++)
{
char ch=[Link](i);
if (ch==' ')
{
st1=st1+' '+st2;
st2="";
}
else
{
st2=ch+st2;
}
}
[Link]([Link]());
}
}

PROGRAM 8:

import [Link].*;
class prog8
{
public static void main()
{
Scanner sc=new Scanner([Link]);
[Link]("Enter Sentence");
String st=[Link]();
[Link]("Enter Word to be searched");
String ws=[Link]();
String st2="";
st=st+' ';
int k=0;
for (int i=0;i<[Link]();i++)
{
char ch=[Link](i);
if (ch==' ')
{
if ([Link](ws)==true)
{
k++;
}
st2="";
}
else
{
st2=st2+ch;
}

}
[Link]("frequency "+k);
}
}

PROGRAM 9:

import [Link].*;
class prog9
{
public static void main()
{
Scanner sc=new Scanner([Link]);
[Link]("Enter Sentence:");
String st=[Link]();
String word="";
for (int i=0;i<[Link]();i++)
{
char ch=[Link](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;
}
[Link](word);
}
}

PROGRAM 10:
DOUBT
PROGRAM 11:

import [Link].*;
class prog11
{
public static void main()
{
int n=0;
Scanner sc=new Scanner([Link]);
[Link]("Enter String: ");
String st=[Link]();
String re="";
for (int i=([Link]())-1;i>=0;i--)
{
char ch=[Link](i);
re=re+ch;

}
if ([Link](re))
[Link]("Palindrome");
else
[Link]("Not palindrome");

}
}

PROGRAM 12:

import [Link].*;
class prog12
{
public static void main()
{
Scanner sc=new Scanner([Link]);
[Link]("Enter String");
String st=[Link]();
String st1="",st2="";
for (int i=0;i<[Link]();i++)
{
char ch=[Link](i);
if
(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u'||ch=='A'||ch=='E'||ch=='I'||ch=='O'
||ch=='U')
{
st1=[Link](0,i);
st2=[Link](i);
break;
}
}
[Link](st2+st1+"AY");
}
}

PROGRAM 13:

import [Link].*;
class prog13
{
public static void main()
{
Scanner sc=new Scanner([Link]);
[Link]("Enter Word");
String st=[Link]();
for (int i=65;i<=90;i++)
{
for (int j=0;j<[Link]();j++)
{
char ch=[Link](j);
if (ch==(char)i||ch==(char)i+32)
{
[Link](ch);
}
}
}
}
}

PROGRAM 14:

import [Link].*;
class prog14
{
public static void main()
{
Scanner sc=new Scanner([Link]);
[Link]("Enter sentence");
String st=[Link]();
st=' '+st;
String st1="";
for (int i=0;i<[Link]();i++)
{
char ch=[Link](i);
if (ch==' ')
{
char ch1=[Link](i+1);
st1=st1+' '+[Link](ch1);
i++;
}
else
st1=st1+ch;
}
[Link]([Link]());
}
}

PROGRAM 15:

import [Link].*;
class prog15
{
public static void main()
{
Scanner sc=new Scanner([Link]);
[Link]("Enter String");
String st=[Link]();
for (int i=1;i<=[Link]();i++)
{
[Link]([Link](0,i));
}
}
}

PROGRAM 16:

import [Link].*;
class prog16
{
public static void main()
{
Scanner sc=new Scanner([Link]);
[Link]("Enter String");
String st=[Link]();
int k=0;
for (int i=[Link]();i>0;i--)
{
for (int j=0;j<k;j++)
{
[Link](' ');
}
[Link]([Link](0,i));
k++;
}
}
}
PROGRAM 17:

import [Link].*;
class prog17
{
public static void main()
{
Scanner sc=new Scanner([Link]);
[Link]("Enter String");
String st=[Link]();
[Link](st);
for (int i=1;i<[Link]()-1;i++)
{
[Link]([Link](i));
for (int j=0;j<[Link]()-2;j++)
[Link](' ');
[Link]([Link]([Link]()-1-i));
}
for (int i=[Link]()-1;i>=0;i--)
{
[Link]([Link](i));
}
}
}

PROGRAM 18:

import [Link].*;
class prog18
{
public static void main()
{
Scanner sc=new Scanner([Link]);
[Link]("Enter Word");
String st=[Link]();
for (int i=0;i<[Link]();i++)
{
[Link]([Link](i,[Link]())+[Link](0,i));
}
}
}

PROGRAM 19:

import [Link].*;
class prog19
{
public static void main()
{
Scanner sc=new Scanner([Link]);
[Link]("Enter Three Letter Word:");
String st=[Link]();
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)
{
[Link]([Link](i)+""+[Link](j)+""+[Link](k));
}
}
}
}
}
}

PROGRAM 20:

import [Link].*;
class prog20
{
public static void main()
{
Scanner sc=new Scanner([Link]);
[Link]("Enter 1. or 2.");
switch([Link]())
{
case 1:
{
[Link]("Enter number of letters:");
int p=[Link]();
for (int i=69;i>=65;i--)
{
for (int j=65;j<=i;j++)
{
[Link]((char)(j));
}
[Link]("");
}
break;
}
case 2:
{
[Link]("Enter Word");
String st=[Link]();
for (int i=0;i<[Link]();i++)
{
for (int j=0;j<=i;j++)
{
[Link]([Link](i));
}
[Link]("");
}
}
}
}
}

PROGRAM 21:

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

PROGRAM 22:

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

PROGRAM 25:

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

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

ARRAY SUMS:

import [Link].*;
class ARRAY1
{
public static void main()
{
Scanner in = new Scanner([Link]);
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++)


{
[Link]("Enter student " + (i+1) + " details:");
[Link]("Marks in English: ");
english[i] = [Link]();
[Link]("Marks in Maths: ");
maths[i] = [Link]();
[Link]("Marks in Science: ");
science[i] = [Link]();
avgMarks[i] = (english[i] + maths[i] + science[i]) / 3.0;
}

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

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


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

import [Link].*;
class ARRAY2
{
public static void main()
{
Scanner sc=new Scanner([Link]);
double arr[]=new double[10];
[Link]("Enter 10 numbers:");
for (int i=0;i<10;i++)
arr[i]=[Link]();
int arr1[]=new int[10];
for (int i=0;i<10;i++)
{
arr1[i]=(int)[Link](arr[i]);
}
for (int i=0;i<10;i++)
[Link](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++)
[Link](arr2[i]);

}
}

import [Link].*;
class ARRAY3
{
public static void main(String args[]){
Scanner in=new Scanner([Link]);
[Link]("Enter numbers");
int a[]=new int[20];
for(int i=0;i<20;i++){
a[i]=[Link]();
}
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];
}
[Link]("Largest number is"+max);
[Link]("Smallest number is"+min);
[Link]("sum is"+sum);
}
}

import [Link].*;
Class ARRAY4
{
public static void main()
{
Scanner sc=new Scanner([Link]);
int p[]=new int[6];
int q[]=new int[4];
int r[]=new int[10];
[Link]("Enter elements in Array P");
for (int i=0;i<6;i++)
p[i]=[Link]();
[Link]("Enter elements in Array Q");
for (int i=0;i<4;i++)
q[i]=[Link]();
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++)
[Link](r[i]);
}
}

Common questions

Powered by AI

The sorting algorithm discussed for Programs 23 and 24 would involve sorting contract details within two arrays, one for names and one for telephone numbers. Though the exact sorting method isn't explicitly detailed, typical approaches like insertion or quicksort could apply. Sorting requires comparing names lexicographically, rearranging both arrays to maintain aligned indices, which ensures name-number pairs remain intact during the sort process .

Program 6 generates initials by traversing the full name, adding the initial characters after spaces, followed by periods, into 'intitials'. After compiling all middle initials, it appends the surname (part after the last space). For 'John Doe Smith', the output is 'J. D. Smith'. A space prefix ensures proper initial collection, handling any leading spaces .

Program 22 stores contacts using parallel arrays where names and telephone numbers share indices between 'arrName' and 'arrTele'. For retrieval, it compares the search name with entries using compareTo(). Upon a match, it outputs the corresponding telephone number. The system supports 20 contacts, handles names lexicographically, and outputs 'Not found' if no match occurs .

Program 8 aims to count the frequency of a specified word in an input sentence, disregarding case. It iterates through the sentence, detecting word boundaries by spaces, accumulating characters into 'st2'. Each compiled word in 'st2' is compared with the search word using equalsIgnoreCase() for case-insensitivity. It counts matches and outputs the frequency .

Program 9 checks each character in the input sentence to determine if it is a consonant (not a vowel). For each consonant, it attempts to replace it with the alphabetically nearest vowel. If the ASCII value manipulation results in a vowel, it uses an incremented ASCII value to maintain consonant continuity. For example, with input 'jump', the output string will be 'jvnq' because 'u' and 'p' are vowels with 'v-' and 'm-' nearest vowels .

Program 7 uses an algorithm that iterates over the input sentence character by character, accumulating characters into a temporary string 'st2'. Once a space is encountered, it adds the reversed 'st2' to the resulting string 'st1', then resets 'st2'. The final output is trimmed to remove leading spaces from the initial ' ' concatenation. It effectively reverses the order of characters in each word while maintaining the original word order. Leading and trailing spaces are removed in the final output by applying trim().

Program 4 converts each character in a string to its opposite case (upper to lower and vice versa). It iterates over each character, checks its ASCII value, and uses Character.toLowerCase() or Character.toUpperCase() functions to convert it. For example, if the input string is 'HeLlo WoRLd', the output will be 'hElLO wOrlD' .

Program 16 constructs a triangular pattern by printing decreasing substrings of the input string (starting from the whole string down to individual characters), with each substring indented progressively to the right. It achieves this by increasing 'k' and printing 'k' spaces before each substring in the decrement loop. This indentation creates the visual effect of a pyramid from left to right .

Program 11 determines if a string is a palindrome by reversing the string and comparing it to the original. It iterates over the string characters from the end to the start, building a reversed copy. It then checks for equality between the original and reversed strings. If they are equal, the input is a palindrome; otherwise, it is not. This requires the string to read the same forward and backward .

Program 12 transforms a word into Pig Latin by locating the first vowel in the word. It splits the word at the vowel's position, appending the prefix (pre-vowel characters) to the suffix (vowel and following characters) and adding 'AY' at the end. If the word starts with a vowel, the entire word is considered a suffix, and an empty prefix is prefixed with 'AY'. Thus, 'apple' becomes 'appleAY' .

You might also like