Home | | Sitemap ||Page number :27

Example 3

//reverse from last number... /*Eg-"India is great"; Print-"great is India";*/ // author Nitish Upreti

class work_on { private String x; private int c,l,t,k,b; // Class constructor

work_on() { x="India is great"; l=x.length(); k=l-1; b=0; }

public void rev() { for(c=l-1;c>=b;c--) {

while(x.charAt(k)!=' ' || k!=b) k--; t=k; while(t<l) { System.out.print(x.charAt(t)); } } } }

class last_rev { public static void main(String args[]) { work_on obj=new work_on(); obj.rev(); } }

Example 4

/* A program to convert a word into pig latin. E.G-- Input=King Output=ngiKay*/ //author Nitish Upreti

import java.io.*; class piglatin { public static void main(String args[]) throws IOException { String word; InputStreamReader rd=new InputStreamReader(System.in); BufferedReader inp=new BufferedReader(rd);

System.out.println("Enter a word"); word=inp.readLine();

word=word.trim();

int c,l=word.length(),pos=0; char ch;

for(c=l-1;c>=0;c--) { ch=word.charAt(c);

if(ch=='a'|| ch=='e'|| ch=='i'|| ch=='o'|| ch=='u') pos=c; }

for(c=pos+1;c<l;c++) System.out.print(word.charAt(c));

for(c=pos;c>=0;c--) System.out.print(word.charAt(c));

System.out.print("ay"); } }

Example 5

/* Reversing a String /* import java.io.*; class rev { public static void main(String args[]) throws IOException { InputStreamReader reader=new InputStreamReader(System.in); BufferedReader input=new BufferedReader (reader); String inp; int c,b=0,e; System.out.println("Enter a String to reverse"); inp=input.readLine(); e=inp.length(); char arr[]=new char[1000]; inp.getChars(b,e,arr,0); System.out.println("**********************"); for(c=e;c>=0;c--) System.out.print(arr[c]); } }

Example 6

/* A program used to search a number /*

class search_word { public static void main(String args[]) { String org="I am slowly improving my skills"; org=org.trim();

String find="my"; String temp=new String();

int c,len=org.length(); int beg=0; boolean swt=false;

for(c=0;c<len;c++) { if(c==len-1 || org.charAt(c+1)==' ') { temp=org.substring(beg,c+1); //System.out.println(temp); if(temp.equals(find)) //Ignore Case Optional swt=true; beg=c+2; } }

if(swt==false) System.out.println("Not Found!!"); else System.out.println("Found!!!"); } }

Example 7

/* A program to print name in short form E.G- Mohandas Karamchandra Ganghi output- M.K. Gandhi*/

//author Nitish Upreti

class shname { String name=new String(); String namelast=new String(); int l,c,k=0,st,t; char ch[]=new char[15];

shname() { name="Bhim Rao Ambedkar"; l=name.length(); c=l-1; }

void workaround() { for(c=l-1;c>=0;c--) { if(name.charAt(c-1)==' ') { st=c; break; } } namelast=name.substring(st,l);

for(c=st-2;c>=0;c--) { if(c==0 || name.charAt(c-1)==' ') { ch[k]=name.charAt(c); k++; } } t=k; }

void print_it() { for(c=t-1;c>=0;c--) { System.out.print(ch[c]+"."); } System.out.print(" "); System.out.print(namelast); }

public static void main(String args[]) { shname st=new shname(); st.workaround(); st.print_it(); } }

Exercise

Q1. How does a String differ from other conventional data type?

Q2. State the difference between the classes “String” and “StringBuffer”

. Q3. What do you understand by the term concatenation of String?

Q4. Explain getChars and getBytes.

Q5. Why two same strings with different cases reported unequal by Java?