0% found this document useful (0 votes)
13 views5 pages

Programs (Strings)

The document contains 9 Java code snippets that perform various string manipulation tasks like word search, word replacement, name formatting, and extracting letters from words. The code uses common techniques like scanning user input, iterating through strings, comparing/extracting substrings, and switching on options. Functions include counting word occurrences, replacing words, shortening names, and printing initial or final letters of words.

Uploaded by

meenakshirayarao
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views5 pages

Programs (Strings)

The document contains 9 Java code snippets that perform various string manipulation tasks like word search, word replacement, name formatting, and extracting letters from words. The code uses common techniques like scanning user input, iterating through strings, comparing/extracting substrings, and switching on options. Functions include counting word occurrences, replacing words, shortening names, and printing initial or final letters of words.

Uploaded by

meenakshirayarao
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

5) import java.util.

*;
class wordSearch
{
public static void main()
{
Scanner ar=new Scanner(System.in);
String s,w="",sw;
int i,l,cnt=0;
char c;
System.out.println("Enter a sentence:");
s=ar.nextLine();
System.out.println("Enter word to check");
sw=ar.nextLine();
s=s+" ";
l=s.length();
for(i=0;i<l;i++)
{
c=s.charAt(i);
if(c!=' ')
w=w+c;
else{
if(w.compareToIgnoreCase(sw)==0)
{
cnt=cnt+1;
}
w="";
}
} System.out.println(cnt);
}
}

6) import java.util.*;
class replace
{
public static void main()
{
Scanner ar=new Scanner(System.in);
String s,w="",rw,rpw;
int i,l;
char c;
System.out.println("Enter a sentence:");
s=ar.nextLine();
System.out.println("Enter word to remove");
rw=ar.nextLine();
System.out.println("Enter word to replace");
rpw=ar.nextLine();
s=s+" ";
l=s.length();
for(i=0;i<l;i++)
{
c=s.charAt(i);
if(c!=' ')
w=w+c;
else{
if(w.compareToIgnoreCase(rw)==0)
{
w=w.replace(w,rpw);
}
System.out.print(w+" ");
w="";
} } }}

7)import java.util.*;
class name
{
public static void main()
{
Scanner ar=new Scanner(System.in);
String s,w="",a="";
int i,l,wl,opn;
char c;
System.out.println("Enter a name:");
s=ar.nextLine();
System.out.println("Enter your option");
System.out.println("1)short name"); System.out.println("2)bibliography
name"); System.out.println("3)initials");
opn=ar.nextInt();
l=s.length();
for(i=0;i<l;i++)
{
c=s.charAt(i);
if(c!=' ')
{ w=w+c;
wl=w.length();
}
else
{
a=a+w.charAt(0)+".";
w="";
}
}
int n=s.lastIndexOf(" ");
switch(opn)
{
case 1:
System.out.println("Short Name:"+a+" "+s.substring(n+1,l));
break;
case 2:
System.out.println("bibliography name:"+s.substring(n+1,l)+", "+a);
break;
case 3:
System.out.println("initials:"+a+s.substring(n+1,n+2)+".");
break;
default:
System.out.println(“No option”);
}
}}
8) import java.util.*;
class name2
{
public static void main()
{
Scanner ar=new Scanner(System.in);
String s,w="",a="";
int i,l,wl;
char c;
System.out.println("Enter a name:");
s=ar.nextLine();
l=s.length();
for(i=0;i<l;i++)
{
c=s.charAt(i);
if(c!=' ')
{ w=w+c;
wl=w.length();
}
else
{
a=a+w+" ";
w="";
}
}
int n = s.lastIndexOf(" ");
System.out.println(s.substring(n+1,l)+" "+a);
}
}

9) import java.util.*;
class letter
{
public static void main()
{
Scanner ar=new Scanner(System.in);
String s,w="",a="";
int i,l,opn;
char c;
System.out.println("Enter a Sentence:");
s=ar.nextLine();
System.out.println("Enter your option");
opn=ar.nextInt();
s=s+" ";
l=s.length();
for(i=0;i<l;i++)
{
c=s.charAt(i);
if(c!=' ')
{ w=w+c;
}
else
{
switch(opn)
{
case 1:
System.out.println(w.charAt(0));
break;
case 2:
System.out.println(w.charAt(w.length()-1));
break;
default:
System.out.println(“No option”);
}
w="";
}
}
}
}

You might also like