Sie sind auf Seite 1von 14

ICSE PROJECT - 3

STRING HANDLING

NAME: MADHURIMA DATTA


CLASS - X

SECTION - B

ROLL NO - 12
SCHOOL: A.G. CHURCH ASANSOL

PROGRAM - 3

PROGRAM 3:

WAP to
1. Accept a sentence and change the alphabets
after vowels into opposite case.
2. Replace the word of your choice with a given
word

VARIABLE LISTING

Serial no
1
2
3
4
5
6
7
8
9
10

PROGRAM

Variable name
ch
ch1
l
i
ctr
s
ns
nsr
wdr
ew

Variable type
char
char
int
int
int
String
String
String
String
String

Variable description
character to check vowel
character to check vowel
length
loop variable
counter variable
string entered
new string
new string replaced
word replaced
entered word

import java.io.*;
class sentence
{
char ch,ch1;
String s,ns,nsr,wdr,ew="";
int l,i,ctr=0;
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
void show()throws IOException
{
System.out.println("Enter a sentence");
s=br.readLine();
s=s.trim();
l=s.length();
x:
for(i=0;i<l;i++)
{
ch=s.charAt(i);
if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u'||ch=='A'||ch=='E'||
ch=='I'||ch=='O'||ch=='U')
{ ch1=s.charAt(i+1);
if(ch1=='a'||ch1=='e'||ch1=='i'||ch1=='o'||ch1=='u'||ch1=='A'||
ch1=='E'||ch1=='I'||ch1=='U')
{
ns=ns+ch;
continue x;
}
if(Character.isLowerCase(ch1))
{
ns=ns+ch+Character.toUpperCase(ch1);
ctr++;
}
if(Character.isUpperCase(ch1))
ctr++;
if(ch1==' ')
{ns=ns+ch+" ";
ctr++;
}
if(ch1==' ')
{ns=ns+ch+" ";ctr++;
}

else
{
if(ctr==0)
ns=ns+ch;
if(ctr>0)
{ctr=0;
continue x;
}
}
System.out.println();
System.out.println("enter the word to be replaced");
wdr=br.readLine();
System.out.println("enter the replacing word");
ew=br.readLine();
for(int i=0;i<l;i++)
{ch=s.charAt(i);
if(ch!=' ')
wdr=wdr+ch;
else
{
if(wdr.equalsIgnoreCase(ew))
nsr=nsr+wdr+" ";
else
nsr=nsr+wdr+" ";
wdr="";
}
}
System.out.println("The string after replacing word : "+nsr);
}
}
}
}

OUTPUT

Enter a sentence:
COMPUTER IS INTERESTING
COmPUtEr Is InTErEsTInG
enter the word to be replaced
COMPUTER
enter the replacing word
MATHEMATICS
The string after replacing word : MATHEMATICS IS INTERESTING

PROGRAM - 7

PROGRAM 7:
WAP to input a sentence and print it in the given
manner:
Example:
JOURNEY BY TRAIN
1.

J.B.T

2.

TRAIN JOURNEY BY

3.

YENRUOJ YB NIART

4.

EJNORUY BY AINRT

5.

ABIJMNORRTUYY

6.

T.B.JOURNEY

7.

Y.Y.N

8.

JOUEBTAI

9. Print the letters after removing the


vowels.
10. Count how many times a word, eg.
BY occurs in the sentence as an
individual word and a part of the sentence

VARIABLE LISTING

Serial no

Variable name

Variable type

Variable description

ch

char

extract character

ch1

char

new character

String

entered sentence

s1

String

string pattern

s2

String

string compare

s3

String

string compare

ns

String

new string

nsr

String

string output pattern

wdr

String

word

10

ew

String

word compare

11

int

loop variable

12

int

loop variable

13

int

counter variable

14

int

stores length

15

int

flag variable

16

int

counter variable

PROGRAM

import java.io.*;
class String_handling
{
char ch,ch1;
String s,s1,s2="",s3="",ns,nsr,wdr,ew="";
int l,i,j,k,ctr=0,f=1,b=0;
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
void show()throws IOException
{
System.out.println("Enter a sentence");
s=br.readLine();
s=s.trim();
l=s.length();
ns=s.charAt(0)+".";
System.out.println("--------OUTPUT PATTERNS---------");
//OUTPUT PATTERN 1
for(i=0;i<l;i++)
{
ch=s.charAt(i);
if(ch==' ')
{
ns+=s.charAt(i+1)+".";
b++;
}
}
System.out.println(ns);
ns="";
//OUTPUT PATTERN 2
for(i=0;i<l;i++)
{
ch=s.charAt(i);
if(ch==' ')
ctr++;
if(ctr==b)
{ ns=s.substring(i+1,l)+ " " + s.substring(0,i);
System.out.println(ns);
break;
}
}

ns="";
//OUTPUT PATTERN 3
s1=s;
s1=s1+" ";
for(i=0;i<=l;i++)
{
ch=s1.charAt(i);
if(ch==' ')
{
s2=s2+" "+ s3;
s3="";
}
else
s3=ch+s3;
}
s2=s2.trim();
System.out.println(s2);
//OUTPUT PATTERN 4
ctr=0;
s2=s;
for(i=0;i<l;i++)
{
ch=s2.charAt(i);
if(ch==' ')
{
s1=s2.substring(0,i);
s2=s2.substring(i);
for(k=65;k<90;k++)
{
for(j=0;j<i;j++)
{
ch=s1.charAt(j);
if(ch==(char)k||ch==(char)(k+32))
System.out.print(ch);
}
}
}
s2=s2+s1;
}

System.out.println();
//OUTPUT PATTERN 5
s1=s;
s1=s1+" ";
for(i=0;i<l;i++)
{
ch=s.charAt(i);
if(ch!=' ')
s1=s1+ch;
}
for(i=65;i<90;i++)
{
for(j=0;j<l;j++)
{
ch=s1.charAt(j);
if(ch==(char)i||ch==(char)(i+32))
System.out.print(ch);
}
}
System.out.println();
//OUTPUT PATTERN 6
for(i=l-1;i>=0;i--)
{
ch=s.charAt(i);
if(ch==' ')
System.out.print(s.charAt(i+1)+".");
}
for(i=0;i<l;i++)
{
ch=s.charAt(i);
if(ch==' ')
{
System.out.print(s.substring(0,i));
break;
}
}
System.out.println();
//OUTPUT PATTERN 7
for(i=0;i<l;i++)
{
ch=s.charAt(i);

if(ch==' ')
ns+=s.charAt(i-1)+".";
}
ns+=s.charAt(l-1)+".";
System.out.println(ns);
ns="";

//OUTPUT PATTERN 8
for(i=0;i<l;i++)
{
f=0;
ch=s.charAt(i);
if(ch!=' ')
{
for(j=0;j<l;j++)
{
if(ch==s.charAt(j))
f++;
}
if(f==1)
ns+=ch;
}
}
System.out.println(ns);
ns="";
ctr=0;
//OUTPUT PATTERN 9
s2="BY";
for(i=0;i<l-1;i++)
{
s1=s.substring(i,i+2);
if(s1.equals(s2))
ctr++;
}
System.out.println("THE NO. OF TIMES 'BY' OCCURS IN GIVEN STRING IS
"+ctr);
//OUTPUT PATTERN 10
s1=s;
for(i=0;i<l;i++)

{
ch=s1.charAt(i);
for(j=i+1;i<l;i++)
{
ch1=s1.charAt(j);
if(ch==ch1&&ch!=32)
s1=s1.substring(0,j-1)+s1.substring(j+1);
}
}
System.out.println(s1);
}
}

OUTPUT
Enter a sentence
JOURNEY BY TRAIN
--------OUTPUT PATTERNS--------J.B.T.
TRAIN JOURNEY BY
YENRUOJ YB NIART
EJNORUY BY AINRT
ABEIJNNORRTUYY
T.B.JOURNEY
Y.Y.N.
JOUEBTAI
JRNY BY TRN
THE NO. OF TIMES 'BY' OCCURS IN GIVEN STRING IS 1

Das könnte Ihnen auch gefallen