Program to find Position of a String in another String.

Java Program


Hi , My today's post is on a program to find  Position of a String in another String.
For Example ,Given two strings, A and B, how to find the first position of B in A? For instance, A = " ab123cdefgcde"; B= "cde" Then the first position of B in A is 6.



public class JavaInHouseRemoveDuplicate{


import java.util.Scanner;

public class SB

{

 public static void main(String[] args) {

  {
   String test=null;
   String index=null;
   int j=0;
   System.out.println("Enter the string");
   test= new Scanner(System.in).nextLine();
   System.out.println("Enter the substring you want to count");
   index= new Scanner(System.in).nextLine();
   for (int i = 0; i <= test.length()-index.length(); i++) {
    String testing=test.substring(i, index.length()+i);
  
    if(testing.equals(index))
    {
     j=i+1;
    System.out.println("It occurs first  at "+ j +" position" );
    break;
    }
   }

  }
 }
}


if you have any questions or suggestions please comment below.

Program to find first position of a String in another string

Hi , My today's post is on a program to find  Position of a String in another String.
For Example ,Given two strings, A and B, how to find the first position of B in A? For instance, A = " ab123cdefgcde"; B= "cde" Then the first position of B in A is 6.





 import java.util.Scanner;

public class SB

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

  {
   String test=null;
   String index=null;
   int j=0;
   System.out.println("Enter the string");
   test= new Scanner(System.in).nextLine();
   System.out.println("Enter the substring you want to count");
   index= new Scanner(System.in).nextLine();
   for (int i = 0; i <= test.length()-index.length(); i++) {
    String testing=test.substring(i, index.length()+i);
 
    if(testing.equals(index))
    {
     j=i+1;
    System.out.println("It occurs first  at "+ j +" position" );
    break;
    }
   }

  }
 }
}
 

Program for Shifting character in a String eg. String "abcde" should be printed as "eabcd"

Shift Character Java Program



import java.util.Scanner;

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

   Scanner in = new Scanner(System.in);
 
         System.out.println("Enter a string to shift characters");
      String  test = in.nextLine();
   
         char abc[]=test.toCharArray();
   
         char ch=abc[0];
         char chi=abc[abc.length-1];
         abc[0]=chi;
         for (int i=0;i<abc.length-1;i++){
          chi=abc[i+1];
 
          abc[i+1]=ch;
          ch=chi;
         }
     
         String result=new String(abc);

        System.out.println(result);
     
     }

}

Program in java to check whether a String is palindrome or not

import java.util.*;
 public class Palindrome
 {
    public static void main(String args[])
    {
       String original, reverse = "";
       Scanner in = new Scanner(System.in);
 
       System.out.println("Enter a string to check palindrome");
       original = in.nextLine();
 
       int length = original.length();
 
     
     
       for ( int i = length - 1 ; i >= 0 ; i-- )
          reverse = reverse + original.charAt(i);
 
       if(reverse.equals(original))
       System.out.println("Yes String is palindrome");
       else
        System.out.println("NO String is not palindrome");
    }
 }

Program in java to split String without using split() method

import java.util.*;
public class SplitString
{
  public static void main(String[] args) {
      String[] s = {"Netherlands_Iceland_Norway_Denmark"};
      String[] finalString = mymethod(s);      
      for (int i = 0; i < s.length; i++) {
          System.out.println("" + finalString[i]);
      }
  }

  static public String[] mymethod(String[] mystring) {
      String ss[] = new String[mystring.length];
      for (int j = 0; j < mystring.length; j++) {
          ss[j] = mystring[j].replace('_', ' ');
      }
      return ss;
  }
 }

Count no.of times a word repeats in String in Java

public class Test

{

public static void main(String[] args) {
String test="Hi I am here am am too good am very a m";
String find="am";
int count=0;

String a[]=test.split(" ");
for (int j = 0; j < a.length; j++) {
if(a[j].equals(find))
{
count++;
}
}


System.out.println(count);

}


}