Search This Blog

Sunday, December 29, 2013

Ques:- A class my_string has been define with a following detail:
  Data member: str,len
  member function:
  1) default constructor()
  2) void readstring()
  3) int code (int index)() return ascii code for the character at position index)
  4) void word(display longest word in the string)
  




/*A class my_string has been define with a following detail:
* Data member: str,len
* member function:
* 1) default constructor()
* 2) void readstring()
* 3) int code (int index)() return ascii code for the character at position index)
* 4) void word(display longest word in the string)
*/
import java.io.*;
class my_string
{
String str;
int len;
public my_string()
{
str="";
len=0;
}
public void readstring()throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter String");
str=br.readLine();
len=str.length();
}
int code(int x)
{
char ch=str.charAt(x);
int ascii=(int)(ch);
return ascii;
}
void word()
{
int i,j,c=0,pos=0,lar=0;
String wd="",arr[]=new String[20];
char ch;
str=str+" ";
len=str.length();
for(i=0;i<len;i++)
{
ch=str.charAt(i);
if(ch!=' ')
{
wd=wd+ch;
}
else
{
arr[c]=wd;
c++;
wd="";
}
}
for(j=0;j<c;j++)
{
if(arr[j].length()>=lar)
{
lar=arr[j].length();
pos=j;
}
}
}
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
{
my_string ob=new my_string();
ob.readstring();
ob.word();
}
}
}

No comments:

Post a Comment