Program on strings?

Discussion in 'Strings, I/O, Formatting, Parsing' started by jagsjava, Jun 14, 2010.

  1. jagsjava

    jagsjava New Member

    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. sreenathmca

    sreenathmca New Member

    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;


    }
     
  3. abhijeetmund

    abhijeetmund New Member

    Re: Program

    can any body tells the solution of this program
    1
    2 2
    3 3 3
    4 4 4 4
     
  4. hari007

    hari007 New Member

    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
     
  5. hari007

    hari007 New Member

    hi try to this one

    for(int i=1;i<=4;i++) {
    for(int j=1;j<=i;j++) {
    System.out.print(i+" ");
    }
    System.out.println();
    }

    -Hari
     
  6. abhijeetmund

    abhijeetmund New Member

    Re: Program on triangle??

    tnxs hari..i got it.
     
  7. abhijeetmund

    abhijeetmund New Member

    Re: Program on triangle shape program??

    1
    1 1
    1 2 1
    1 3 3 1
    1 4 6 4 1
     
  8. gmsreddi

    gmsreddi New Member

    input is my name is madhu

    output os madhu is name my

    How to write the program in java
     
  9. ramsri4ever

    ramsri4ever New Member


    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. hari007

    hari007 New Member

    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
     
  11. hari007

    hari007 New Member

    that is done by calling String reverse method.
     
  12. l.pradeepkumar.l

    l.pradeepkumar.l New Member

    Re: Program

    for(int i=1;i<=4;i++){
    for(int j=1;j<=i;j++)
    System.out.print(i);
    System.out.println();
     
  13. chauhan.prakash01

    chauhan.prakash01 New Member

    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: Jul 4, 2010
  14. chauhan.prakash01

    chauhan.prakash01 New Member

    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: Jul 4, 2010
  15. alphonse

    alphonse New Member

    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. Konduruchaitanya

    Konduruchaitanya New Member

    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. Konduruchaitanya

    Konduruchaitanya New Member

    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. siva.sreerama

    siva.sreerama New Member

    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. sathish2009.java

    sathish2009.java New Member

    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. AnanthNag_KUNDANALA

    AnanthNag_KUNDANALA New Member

    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"));
    }
    }
     

Share This Page