Showing posts with label Search. Show all posts
Showing posts with label Search. Show all posts

Friday, 14 June 2013

Searching String - lastIndexOf()

To search for the last occurence of a specific character  in the string , lastIndexOf() function is used.This function returns an integer type value ,it returns the last occurrence of the specified character in the string.The charcter should be written in single quote
Syntax:    Stringname.lastIndexOf('ChararacterToBeSearched');

The same function can be used to search for a particular string in a complete string.The string is always written in double quotes.This function will return he last occurence of the string in the complete string.If the string 
Syntax :   Stringname.lastIndexOf("StringToBeSearched");

If you want to search for a character after a particular number of characters, then the start index is also given along with the character, when the program is compiled by Java complier , compiler donot search for the specified character upto that index number specified by you.
Syntax:  Stringname.lastIndexOf('ChararacterToBeSearched',StartIndex);

In the same way if you want to search a string or a word in the string to be searched then the start index is given along with the string to be searched. Compler during compiling donot search forthe string upto that index numbered entered.
Syntax::   Stringname.lastIndexOf("StringToBeSearched",StartIndex);

Program for lastIndexOf()


class Last
{
public static void main(String arg[]) 
{
String s= "this is the duty of every citizen of india to serve his country at the cost of anything";
System.out.println(s);

System.out.println(s.indexOf('o'));
System.out.println(s.lastIndexOf('o'));

System.out.println(s.indexOf("the"));
System.out.println(s.lastIndexOf("the"));

System.out.println(s.indexOf("o",10));
System.out.println(s.lastIndexOf('o',60));

System.out.println(s.indexOf("the",10));
System.out.println(s.lastIndexOf("the",60));

}
}

Output of the above code is :


Thursday, 13 June 2013

Searching strings- indexof() method

The string class provide two methods that allow to search a string for a specific character or substring...

indexof();

lastIndexOf();



To search for a specific character  in the string , indexOf() function is used.This function returns an integer type value ,it returns the first occurrence of the specified character in the string.The charcter should be written in single quote
Syntax:    Stringname.indexOf('ChararacterToBeSearched');

The same function can be used to search for a particular string in a complete string.The string is always written in double quotes.
Syntax :   Stringname.indexOf("StringToBeSearched");

If you want to search for a character after a particular number of characters, then the start index is also given along with the character, when the program is compiled by Java complier , compiler donot search for the specified character upto that index number specified by you.
Syntax:  Stringname.indexOf('ChararacterToBeSearched',StartIndex);

In the same way if you want to search a string or a word in the string to be searched then the start index is given along with the string to be searched. Compler during compiling donot search forthe string upto that index numbered entered.
Syntax::   Stringname.indexOf("StringToBeSearched",StartIndex);

Program to demonstrate the use of indexOf() unction

import java.io.*;
import java.util.*;
class Searchstr
{

public static void main(String arg[])
{
String s= "this is the duty of every citizen of india to serve his country at any cost";
System.out.println(s);

System.out.print("\n\nIndex of(o) = ");
System.out.println(s.indexOf('o'));

System.out.print("Index of(the) = ");
System.out.println(s.indexOf("the"));

System.out.print("Index of('o',10) = ");
System.out.println( s.indexOf('o',10));

System.out.print("Index of(the,10) = ");
System.out.println( s.indexOf("the",10));

}
}

Below is the output of above code:



Thursday, 23 May 2013

Binary Search

Binary search technique is more efficient than the linear search. In binary search each and every element is not traversed to find the required element.
The main limitation of binary search technique is that it can process only sorted data.The data need to be in sorted form so that binary search can be applied to it.If data is not sorted then it needs to be sorted first.
In Binary search the middle element is found first. Basically there are three values required.
Upper_bound (ub),Lower_bound(lb),Middleelemnt(mid)
mid=(ub+lb)/2;
if the middle element is less than the element to be searched then
lb=mid+1..
 and id middle element is greater than the element to be searched then.
ub=mid-1;

Program for Binary Search

#include<stdio.h>
#include<conio.h>
void main()
{
int a[10];
int flag,n,i,item,mid,lb,ub;
clrscr();
printf("Enter the number of elements you wnat to enter");
scanf("%d",&n);
printf("\nEnter the elements of arrayin sorted form\n");
for(i=0;i<n;i++)
{
 printf("a[%d] =",i);
 scanf("%d",&a[i]);
}

printf("\nThe elements of array are");
for(i=0;i<n;i++)
{
 printf("\na[%d] = %d",i,a[i]);
}

printf("\nEnter the elements of array to be searched");
scanf("%d",&item);

lb=0;
ub=n-1;
while(ub>=lb)
{
mid=(ub+lb)/2;

if(item==a[mid])
{
flag=1;
break;
}
else
{
if(item>a[mid])
{
 lb=mid+1;
}
else
{
ub=mid-1;
}
}
}
if(flag==1)
{
printf("\n Item %d found at location a[%d]",item,mid);
}
else
{
printf("\nItem not found");
}
getch();
}

                                 

Wednesday, 22 May 2013

Linear search

Linear search is one of the searching technique using a simple logic of matching each element of the array with the item to be searched.Linear search can be performed on any type of array.. the array may be sorted or unsorted..it works on both.
This method s not efficient when the number of elements is very large. When processing huge amount of data this method gets very slow because it is very time and resource  consuming as each and every element is traversed thus is not used for processing huge data.


#include<stdio.h>
#include<conio.h>
void main()
{
int a[10];
int n,i,item,k=0,j;
clrscr();
printf("Enter the number of elements you wnat to enter");
scanf("%d",&n);
printf("\nEnter the elements of array\n");
for(i=0;i<n;i++)
{
 printf("a[%d] =",i);
 scanf("%d",&a[i]);
}

printf("\nThe elements of array are");
for(i=0;i<n;i++)
{
 printf("\na[%d] = %d",i,a[i]);
}

printf("\nEnter the elements of array to be searched");
scanf("%d",&item);
for(k=0;k<n;k++)
{
if(a[k]==item)
{
j=1;
break;
}
}
if(j==1)
printf("success");
else
printf("no success");
getch();
}
full details in json
Proudly Powered by Blogger.