Program to check whether a given character is present in a string or not
In this article, you will understand the logic to find out whether the given character is present in the string or not and find out the position of the specified character in the string. We will use the Java programming language to execute program. Additionally, you can also use other programming language by following same logic to get output.
Approach
The main logic to the given problem is you need to traverse the string by comparing each character of the string with the required character. If you find the required character, you need to output the message saying whether the character is present in the string or not.
Example
import java.io.* ;
import java.util.*;
public class Main
{
public static void main (String [] args) {
Scanner sc = new Scanner (System.in);
int found = 0;
// found is used to give the output the index of required character in the string
System.out.println(" enter the String");
String s = sc.next ();
// string input will be given by the user
System.out.println("enter the character to find from the string ");
char c=sc. next () .charAt (0);
// character input will be given by the user
for (int i = 0; i < s.length (); i++)
{
if (c == s.charAt (i))
// traversing the string and comparing it with the required character
{
found = i;
// if the character is found then found will be updated to i and will exit the loop break;
}
}
if (found!=0)
{
System.out.print ("character is present in the string at the index = " +found);
// output message
}
else
{
System.out.print ("character is not present in the string");
// output message
}
}
}
Output


Find the position of a given character in the string
The given problem statement is that you will be given a string. You will have to find the position of the character presented in the given string. We will use the Java programming language to execute program. Additionally, you can also use other programming language by following same logic to get output.
Approach
The main logic to the given problem is you need to traverse the string by comparing each character of the string with the required character. If you find the character, then you need to output the index of that character in the string.
Example
import java.io.* ;
import java.util.*;
public class Main
{
public static void main (String [] args) {
Scanner sc = new Scanner (System.in);
int found = 0;
// found is used to give the output the index of required character in the string
System.out.println(" enter the String");
String s = sc.next ();
// string input will be given by the user
System.out.println("enter the character to find from the string ");
char c=sc. next () .charAt (0);
// character input will be given by the user
for (int i = 0; i < s.length (); i++)
{
if (c == s.charAt (i))
// traversing the string and comparing it with the required character
{
found = i;
// if the character is found then found will be updated to i and will exit the loop break;
}
}
if (found!=0)
{
System.out.print ("character is present in the string at the index = " +found);
// output message
}
else
{
System.out.print ("character is not present in the string");
// output message
}
}
}

Suppose the required character from the string is repeated. Then the index of the first occurrence will be printed on the screen.