0% found this document useful (0 votes)
3 views

2023 String

The document contains multiple Java programs that perform different string manipulations. These include finding the longest and smallest words in a sentence, reversing words in a sentence, shifting consonants followed by vowels, and printing the initials of words. Each program is accompanied by sample input and output for clarity.

Uploaded by

Ashish Rajput
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)
3 views

2023 String

The document contains multiple Java programs that perform different string manipulations. These include finding the longest and smallest words in a sentence, reversing words in a sentence, shifting consonants followed by vowels, and printing the initials of words. Each program is accompanied by sample input and output for clarity.

Uploaded by

Ashish Rajput
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
You are on page 1/ 5

Longest Word

Sample i/p : today is not sunday


Sample o/p : longest_word =sunday maxlen=6
import java.util.*;
class LONGEST_WORD
{
public static void main(String args[])
{
String word="",maxword=""; int len=0;
System.out.println("enter a string");
Scanner sc=new Scanner(System.in);
String s1=sc.nextLine();
s1=s1.trim(); s1=s1+' ';
int maxlen=0;
for(int i=0;i<s1.length();i++)
{
char ch=s1.charAt(i);
if(ch!=' ')
{
word=word+ch;
}
else
{
len=word.length();
if(len>maxlen)
{
maxlen=len;
maxword=word;
}
word="";
}
}
System.out.println("longest_word ="+maxword+" " +"maxlen="+maxlen);
}
}
Smallest Word
Sample i/p : today is not sunday
Sample o/p : smallest word=is length=2
import java.util.*;
class smallest_word
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("enter name");
String s=sc.nextLine();
int small_length=s.length(),length=0;
s=s+" ";
String s1="",samllest_word="";
for(int i=0;i<s.length();i++)
{
char ch=s.charAt(i);
if(ch!=' ')
{
s1=s1+ch;
}
else
{
length=s1.length();
if(length<small_length)
{
small_length=length;
samllest_word=s1;
}
s1="";
}
}
System.out.println("smallest word="+samllest_word+"
"+"length="+small_length);
}
}
Reverse word in a sentence

Sample i/p : today is not sunday


Sample o/p: yadot si ton yadnus
import java.util.*;
class reverse_word_sentence
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("enter sentence");
String s=sc.nextLine();
s=s+" ";
String g="";
for(int i=0;i<s.length();i++)
{
char ch=s.charAt(i);
if(ch!=' ')
{
g=ch+g;
}
else
{
System.out.print(g+" ");
g="";
}
}
}
}
WAP to shift consonant followed by vowel
Sample i/p : computer
Sample o/p : cmptroue
import java.util.*;
class Test
{
public static void main(String[] args)
{
Scanner sc=new Scanner( System.in);
System.out.println("enter a word");
String s=sc.nextLine();
s=s.toLowerCase(); // solve according to question
String consonant="",vowel="";
for(int i=0;i<s.length();i++)
{
char ch=s.charAt(i);
if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u')
{
vowel=vowel+ch;
}
else
{
consonant=consonant+ch;
}
}
System.out.println(consonant+vowel);
}
}
WAP to print initials of word
Sample i/p: facebook snapcaht instagram
Sample o/p: f s i
import java.util.*;
class dj
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a String");
String st=sc.nextLine();
st=st.trim();
st=' '+st;
for(int i=0;i<st.length();i++)
{
char x=st.charAt(i);
if(x==' ')
System.out.print(st.charAt(i+1)+" ");
}
}
}

You might also like