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

String Programs (Set 2)

This document contains 11 Java programs that perform string manipulation tasks like name swapping, initial extraction, vowel removal, character frequency counting, ASCII value printing, and word worth calculation. The programs take a string as input, perform some operation on it related to the specific task, and output the result.

Uploaded by

sharonvk06
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

String Programs (Set 2)

This document contains 11 Java programs that perform string manipulation tasks like name swapping, initial extraction, vowel removal, character frequency counting, ASCII value printing, and word worth calculation. The programs take a string as input, perform some operation on it related to the specific task, and output the result.

Uploaded by

sharonvk06
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

SECOND SET OF STRING PROGRAMS

1./*Pgm to interchange the surnames of two names


Eg- input
JOHN SMITH
MARK ANTONY
OUTPUT
JOHN ANTONY
MARK SMITH*/

import java.util.*;
public class Last_Name_Swaper
{
public void main()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the 1st full name.");
String a =sc.nextLine();
System.out.println("Enter the 2nd full name.");
String b = sc.nextLine();
int p=a.indexOf(' ');
int q=b.indexOf(' ');
String c=a.substring (0,p)+b.substring(q);
String d=b.substring (0,q)+a.substring(p);
System.out.println("Swapped name 1 = "+ c);
System.out.println("Swapped name 2 = "+ d);
}
}

2./*Pgm to interchange the name and surname


Eg-
Input MINNAL MURALI
Output MURALI MINNAL*/

import java .util.*;


public class Surname

public void main()

Scanner sc=new Scanner(System.in);

System.out.println("Enter the String having 2 parts");

String a=sc.nextLine();

int p= a.indexOf(' ');

System.out.print(a.substring(p)+" "+a.substring(0,p));

3./*Pgm to create initials


Eg ADITHYA SUNIL NAIR
A.S.NAIR

Eg To print Mohandas Karamchand Gandhi as M.K.Gandhi*/

import java .util.*;

public class initialpgm

public void main()

Scanner sc=new Scanner(System.in);


System.out.println("Enter the first String having 3 parts");

String a=sc.nextLine();

System.out.print(a.charAt(0)+".");

int p=a.indexOf(' ');

int q=a.lastIndexOf(' ');

System.out.print(a.charAt(p+1)+".");

System.out.print(a.substring(q));

4)//To print This is a Cat as T.I.A.C

import java .util.*;

public class initialpgm1

public void main()

Scanner sc=new Scanner(System.in);

System.out.println("Enter the String");

String a=sc.nextLine();

a=a.trim();//To delete the leading and trailing blank spaces in a


sentence

String ns="";a=" "+a;

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

if(a.charAt(i)==' '&& a.charAt(i+1)!=' ')

ns=ns+Character.toUpperCase(a.charAt(++i))+".";

System.out.println(ns);

}}

5)/** Write a program in java to accept a string/word and display the


new string after removing all the vowels present in it.

SampLe i/p: COMPUTER

Sample o/p: CMPTR **/

import java.util.*;

public class Q4

public void main()

String w="";

Scanner sc=new Scanner(System.in);

System.out.println("Enter a String");

String st=sc.nextLine();

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

{
char x=Character.toUpperCase(st.charAt(i));

if(x=='A' || x=='E' || x=='I' || x=='O' ||x=='U' )

continue;

else

w=w+x;

System.out.println(w);

6)/* Write a program to accept a word & convert it into lower case, if it
is in upper case. Display a new word by replacing only the vowels
with the character following it.*/

Sample i/p: Computer

Sample o/p: cpmpvtfr */

import java.util.*;

public class Q22

public void main()

char x;

Scanner sc= new Scanner(System.in);

System.out.println("enter a string");
String st=sc.nextLine();

st=st.toLowerCase();

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

x=st.charAt(i);

if(x=='a'||x=='e'||x=='i'||x=='o'||x=='u')

st=st.replace(x,++x);

System.out.println(st);

7)/** Write a program in java to accept a string/sentence and find the


frequency of given alphabets.

Sample i/p: Success

Enter an alphabets whose frequency is to be checked :s

Sample o/p: The frequency of 's’ is 3* */

import java.util.*;

public class Q8

public void main()


{

int c=0;

Scanner sc=new Scanner(System.in);

System.out.println("Enter a String");

String st=sc.nextLine();

System.out.println("Enter an alphabets whose frequency is


to be checked: ");

char ch=sc.next().charAt(0);

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

char x=st.charAt(i);

if(x==ch)

c++;

System.out.println("The frequency of "+ch+" is "+c);

8)/*Frequency of double letter in a string/word

Eg:Rabbit

Fedding

import java.util.*;
public class DoubleLetterSeq

public void main() {

Scanner sc = new Scanner(System.in);

System.out.print("Enter string: ");

String s = sc.nextLine().toUpperCase();

int c=0;

int len = s.length();

for (int i = 0; i < len - 1; i++)

if (s.charAt(i) == s.charAt(i + 1))

c++;

System.out.println("Double Letter Sequence Count = " + c);

9)/** Write a program to accept a word and display the ASCII of each
character.

Sample i/p: BLUEJ


Sample o/p: ASCII of B = 66

ASCII of L = 75

ASCII OF U = 84

ASCII OF E = 69

ASCII of J = 73 * */

import java.util.*;

public class Q13

public void main()

Scanner sc=new Scanner(System.in);

System.out.println("enter a word");

String st=sc.next();

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

char x=st.charAt(i);

System.out.println("ASCII of "+x+" = "+(int)x);

}
10)//Pig Latin word

/*To Convert a word to Pig Latin word.Pig Latin is a langage game of


alterations made in English.

* To form the Pig Latin word of an English word the initial consonant
sound is transposed to the end of the word

* and an "ay" is affixed

Eg:

trash will yield ash

Output trash will yield ashtray

under will yield underplay*/

import java.util.*;

public class Piglatin

public void main()

String b="";int i;

Scanner sc= new Scanner(System.in);


System.out.println("Enter the String");

String a = sc.nextLine().toUpperCase();

for(i=0;i<a.length();i++)

char x=a.charAt(i);

if(x=='A'||x=='E'||x=='I'||x=='O'||x=='U')

break;

b=b+a.substring(i)+a.substring(0,i)+"AY";

System.out.println("PigLatin: "+b);

11)/*To print the worth of a word

i.e Worth of a word = Sum of positions of letters in the English


Alphabet

eg: Worth of the word "Hi" = 8+9=17*/

import java.util.*;

public class Worth_of_a_word

public void main()

Scanner sc = new Scanner(System.in);


System.out.println("Enter the word");

String a= sc.nextLine();

int i,l=a.length(),worth=0;

for(i=0;i<l;i++)

char ch=a.charAt(i);

if(ch>='A'&& ch<='Z')

worth+=(int)(ch-64);

else

worth+=(int)(ch-96);

System.out.println("Worth ="+worth);

You might also like