Program on strings?

#1
write a function strstr(str1,str2) that must returns true if string str1 is ended with string str2 otherwise false.
Eg:If str1=ABCDE and str2=CDE the function must return true, if str2=FGH it must return false
 
#2
public boolean str(String s1,String s2)
{
char c=s1.charAt(s1.length()-1);
char c1=s2.charAt(0);
int count=0;
for(int i=0;i<s2.length();i++)
{
if(c1==c+1)
{
count++;
if(i<s2.length()-1)
{
c=s2.charAt(i);
c1=s2.charAt(i+1);
}
}

}
if(count==s2.length())
return true;
else
return false;


}
 
#4
hi
we have also one predefined method , try to this one

boolean res=str1.endWith(str2);

if str1 ends with str2 res returns true else it returns false.

-Hari
 
#9
input is my name is madhu

output os madhu is name my

How to write the program in java

Hi dude,

I hope this may clear your doubt.

import java.util.ArrayList;
import java.util.Collections;
class Reverse
{
public static void main(String[] args)
{
ArrayList arrayList = new ArrayList();
arrayList.add("my");
arrayList.add("name");
arrayList.add("is");
arrayList.add("madhu");
System.out.println("before reverse::" +arrayList);
Collections.reverse(arrayList);
System.out.println("after reverse ::" +arrayList);
}
}


Regards,
ramsri4ever

 
#10
hi this is one of the solution of Triangle

import java.io.*;
class Triangle {
public static void main(String args[])throws IOException {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Entert any no");
int re=Integer.parseInt(br.readLine());
int a[]=new int[re+1];
int b[]=new int[re+1];
a[0]=1;
for(int i=0;i<re;i++) {
for(int j=1;j<=i+1;j++) {
if(i==0)
break;
else a[j]=b[j-1]+b[j];
}
for(int j=0;j<a.length;j++) b[j]=a[j];
for(int m=re;m>i;m--)System.out.print(" ");
for(int j=0;j<a.length;j++) {
if(a[j]==0) continue;
System.out.print(a[j]+" ");
}
System.out.println();
}
}
}

I think you got it.

-Hari
 
#13
if you want to check whether string1 ends with string2 then first calculate the length of the string2 and get last characters of the first string into a character array. now it is simple
, compare the contents of string2 and the character array

for example, if str1 = "prakash"
and str2= "ash"
then take the last three characters (i.e length of str2) from the string str1 and then compare them with str2. if they are equal, return true otherwise false.


code:-

public boolean strstr(String str1, String str2){

char[] c1= new char[str2.length()]; // make a character array whose length is equal to the length of str2

int startIndex = (str1.length()-str2.length());
int endIndex = str1.length();

str1.getChars(startIndex , endIndex, c1, 0);
// take last characters equal to the length of str2 in char array c1

//Now compare the contents of the array with str2. if they are not equal return false else true
for(int i=0 ; i<str2.length() ; i++){
if(charStr1 != str2.charAt(i))
return false;
}
return true;
}
 
Last edited:
#14
Re: Program

public void makeTriangle(){

int[][] a= new int[4][4];

for(int i=0 ; i<4 ; i++){
for(int j=0 ; j<=i; j++){
System.out.print((a[j]=(i+1))+" ");
}
System.out.println("\n");
}
}
 
Last edited:
#15
For me, the term means to accept the entry of certain rivers, the keyboard, the input file or not is difficult to code with a static rope. MakeWords () should not have three interceptions, take three strings.There also is a logic error in your code because lines 14-15 don't reverse
a string, e g. the string "abcd" is turned into "dccd" and not what you wrote.
 
#16
Answer for gms reddi Question:


sol:
import java.io.*;
class example1
{
public static void main(String a[]) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter your Query");
String s=br.readLine();
String as[];
as=s.split(" ");
System.out.println(" The result is:");
for(int i=(as.length-1);i<=as.length;i--)
{
System.out.print(as+" ");
if(i==0)
{
break;
}
}
}
}

I think U satisified

Regards
Chai2
 
#17
Ansewer for GMS Reddy Question with bes tof my knowledge

import java.io.*;
class example1
{
public static void main(String a[]) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter your Query");
String s=br.readLine();
String as[];
as=s.split(" ");
System.out.println(" The result is:");
for(int i=(as.length-1);i<=as.length;i--)
{
System.out.print(as+" ");
if(i==0)
{
break;
}
}
}
}

Hope u Satisified

Regards:
-------------------
Chai2
 
#18
Hi friends This is program for Reverse String Words



import java.util.StringTokenizer;

class ReverseString
{
public static void main(String[] args)
{

String str=new String("\nCUTE KAVI is SMS Best Friend");
System.out.println("Originalal String:\n\t"+str);

String str2=new String();
StringTokenizer stk=new StringTokenizer(str," ");
while (stk.hasMoreTokens())
{
str2=stk.nextToken()+" "+str2;
}
System.out.println(str2);
}
}


u will get out put:
Friend Best SMS is KAVI CUTE .
 
#19
import java.util.ArrayList;
import java.util.Collections;
class Reverse
{
public static void main(String[] args)
{
ArrayList arrayList = new ArrayList();
String str = "my name is madhu";
String[] s = str.split(" ");
for (int i=s.length-1; i>=0;i--){
arrayList.add(s);

arrayList.add(" ");
}
System.out.println("after reverse ::" +arrayList);
}
}


Regards,
sathishreddy
 
#20
public class SubStringDecider {

boolean Subsub(String str1, String str2) {
boolean flag = false;
if (str1.endsWith(str2)) {
flag = true;
}
return flag;
}

public static void main(String argc[]) {
SubStringDecider decider = new SubStringDecider();
System.out.println("" + decider.Subsub("abcde", "de"));
}
}
 
Top