Hi Guys ,Welcome
Am giving you an intresting program here called "String Chain" .Before that we need to know what is string chaining???well,
it's a process of chaining Strings baced on some specific characters arrangement.
for example:
let we have strings like "shashi","shankar","rock","kiran","tail".Here chaining is done if and only if
last character of first string is equal to the first character of next string in an array.
now apply chain rule for above strings it becomes:
shankar-->rock--->kiran-->null (here null means there is no further names to place in this chain).
I hope you would understand the concept..................lets move to code.
solution:
package logics;
import java.util.*;
public class StringChain {
public static void main(String []args)
{
String []data={"shashi","shankar","rock","kiran","tail"};
int index=0;
int length1=data.length;
ArrayList<String>list=new ArrayList<String>();
String []res=new String[20];
for(int i=0;i<length1;i++)
{
int endlength=data[i].length()-1;
char end=data[i].charAt(endlength);
for(int j=i+1;j<length1;j++)
{
char start=data[j].charAt(0);
if(end==start)
{
res[index++]=data[i];
res[index++]=data[j];
}
}
}
for(String str:res)
{
if(list.contains(str))
{
}
else
{
list.add(str);
}
}
Iterator<String> ite=list.iterator();
while(ite.hasNext())
{
System.out.print(ite.next()+"-->");
}
}
}
output:
===============================
shankar-->rock-->kiran-->null-->
Am giving you an intresting program here called "String Chain" .Before that we need to know what is string chaining???well,
it's a process of chaining Strings baced on some specific characters arrangement.
for example:
let we have strings like "shashi","shankar","rock","kiran","tail".Here chaining is done if and only if
last character of first string is equal to the first character of next string in an array.
now apply chain rule for above strings it becomes:
shankar-->rock--->kiran-->null (here null means there is no further names to place in this chain).
I hope you would understand the concept..................lets move to code.
solution:
package logics;
import java.util.*;
public class StringChain {
public static void main(String []args)
{
String []data={"shashi","shankar","rock","kiran","tail"};
int index=0;
int length1=data.length;
ArrayList<String>list=new ArrayList<String>();
String []res=new String[20];
for(int i=0;i<length1;i++)
{
int endlength=data[i].length()-1;
char end=data[i].charAt(endlength);
for(int j=i+1;j<length1;j++)
{
char start=data[j].charAt(0);
if(end==start)
{
res[index++]=data[i];
res[index++]=data[j];
}
}
}
for(String str:res)
{
if(list.contains(str))
{
}
else
{
list.add(str);
}
}
Iterator<String> ite=list.iterator();
while(ite.hasNext())
{
System.out.print(ite.next()+"-->");
}
}
}
output:
===============================
shankar-->rock-->kiran-->null-->
Không có nhận xét nào:
Đăng nhận xét