Find all occurrence of character in the string

Program to find index of particular character occur throughout the string.

Index tell where the character occur.

void Findindex(char c,int n)

{

int n=0;

int result;

String str=”roses”;

while(result>0)

{

result=str.indexOf(c,n);

System.out.println(result);

n=result+1;

}

}

invoking the method

p.Findindex('s',0);

output of the above program:

2

4

-1

Know how the indexOf method is used in java.

character 's' occur at two position in a string, when loop executes third time it prints -1.

Explanation:

indexOf() method find the character 's' in a string, starting from index zero.

If found, returns the index of character 's'.

Returns -1, if the character 's' is not found.