[go: up one dir, main page]

0% found this document useful (0 votes)
219 views14 pages

String Programs Class 10 Icse

The document contains a series of Java programs that demonstrate various string manipulation techniques, including generating patterns, swapping names, creating initials, and converting strings to Pig Latin. Each program includes code snippets and example inputs and outputs to illustrate their functionality. The programs cover a range of tasks such as removing vowels, calculating ASCII values, and determining the worth of a word based on letter positions in the alphabet.
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)
219 views14 pages

String Programs Class 10 Icse

The document contains a series of Java programs that demonstrate various string manipulation techniques, including generating patterns, swapping names, creating initials, and converting strings to Pig Latin. Each program includes code snippets and example inputs and outputs to illustrate their functionality. The programs cover a range of tasks such as removing vowels, calculating ASCII values, and determining the worth of a word based on letter positions in the alphabet.
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/ 14

String pgms second set

1.Pgm to get the pattern


J
JA
JAV
JAVA
//to print the pattern
import java.util.*;
public class pattern1
{
public void main()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the String");
String a = sc.nextLine();
int l = a.length();
for(int i=0;i<=l;i++)
System.out.println(a.substring(0,i));
}}
2.Pgm to get the pattern
JAVA
JAV
JA
J

//to print the pattern


import java.util.*;
public class pattern2
{
public void main()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the String");
String a = sc.nextLine();
int l = a.length();
for(int i=l;i>=1;i--)
System.out.println(a.substring(0,i));
}
}

3.Pgm to get the pattern


A
VA
AVA
JAVA
//PGM to print the pattern
import java.util.*;
public class pattern3
{
public void main()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the String");
String a = sc.nextLine();
int l = a.length();
for(int i=l;i>=0;i--)
System.out.println(a.substring(i));
}
}
4.Pgm to get the pattern
JAVA
AVA
VA
A
//to print the pattern
import java.util.*;
public class pattern4
{
public void main()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the String");
String a = sc.nextLine();
int l = a.length();
for(int i=0;i<=l;i++)
System.out.println(a.substring(i));

}
}

5.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);
}
}

6.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));

7.Pgm to create initials .

Eg ADITHYA SUNIL NAIR

A.S.NAIR
Another eg - To print Mohandas Karamchand Gandhi as M.K.Gandhi
//Pgm to create initials

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));

}}

8) pgm to print shortform

Eg Input-This is a cat

Output - T.I.A.C.

//To print shortform of a string

import java .util.*;

public class shortform

public void main()

Scanner sc=new Scanner(System.in);

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

String a=sc.nextLine();

String ns="";

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

a=" "+a;//to add a space in the beginning


int l=a.length(); //to find the length of the string

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

if(a.charAt(i)==' ')

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

System.out.println(ns);

}}

9)pgm to print piglatin string.(consonants at the beginning will be transferred to


the last word +"AY")

Eg -input: Trash

output : ASHTRAY

//pgm to to print piglatin string

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();

a=a.toUpperCase();
int l=a.length();

for(i=0;i<l;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);

}}

10) 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 b="";

Scanner sc=new Scanner(System.in);

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

String a=sc.nextLine();

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

char x=Character.toUpperCase(a.charAt(i));

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

continue;

else

b=b+x;

System.out.println(b);

11) 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 k;

Scanner sc= new Scanner(System.in);

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

a=a.toLowerCase();

int l=a.length();

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

k=a.charAt(i);

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

a=a.replace(k,++k);

System.out.println(a);

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

Eg: input Rabbit

Output

Frequency of double letter in the string is=1

Input - Mummy was feeding the rabbit

Output

Frequency of double letter in the string is=3

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 l = s.length();

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

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

c++;

System.out.println("Frequency of Double Letter in the string = " + c);

}}

13)pgm to accept a string ‘ January 26 is celebrated as Republic day of India’

And Generate the output using replace function

August 15 is celebrated as Independence day of India

//To change 26 to 15, January to August and Republic to Independence

import java.util.*;

public class Q21

public void main()

{
Scanner sc=new Scanner(System.in);

System.out.println("Enter a string:");

String st=sc.nextLine();

st=st.replace("January","August");

st=st.replace("26","15");

st=st.replace("Republic","Independence");

System.out.println(st);

14)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

// ASCII value of each character

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);

15)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

//Pgm to print worth of a word

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); or (int)ch-64; or ( int)(ch-64);

else

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

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

Same pgm another method

import java.util.*;

public class Worth_of_a_word1

public void main()

Scanner sc = new Scanner(System.in);

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

String a= sc.nextLine();

a=a.toUpperCase();

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

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

{
char ch=a.charAt(i);

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

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

You might also like