Program Discussion :: Basics
5 / 279
Write a program to print the desired character from a string or search a char in a string.
Answer:
#include
#include
#include
using namespace std;
int main()
{
char a, word[50];
int i, freq = 0, flag = 0;
printf("Enter character: ");
scanf("%c", &a);
printf("Now enter the word: ");
scanf("%s", word);
printf("Positions of '%c' in %s are: ", a, word);
for (i = 0; i < strlen(word); i++)
{
if (word[i] == a)
{
flag = 1;
printf("%d ", i + 1);
freq++;
}
}
if (flag)
{
printf("\nCharacter '%c' occured for %d times.\n", a, freq);
}
else
{
printf("None\n");
}
return 0;
}
int main()
{
char a, word[50];
int i, freq = 0, flag = 0;
printf("Enter character: ");
scanf("%c", &a);
printf("Now enter the word: ");
scanf("%s", word);
printf("Positions of '%c' in %s are: ", a, word);
for (i = 0; i < strlen(word); i++)
{
if (word[i] == a)
{
flag = 1;
printf("%d ", i + 1);
freq++;
}
}
if (flag)
{
printf("\nCharacter '%c' occured for %d times.\n", a, freq);
}
else
{
printf("None\n");
}
return 0;
}
Asked In ::
Language:

Rai
3 May, 2017 12:22 PM
import java.util.*;
import java.io.*;
import java.lang.*;
class SearchChar
{
public static void main (String[] args)
{
int position=0;
System.out.println("Enter The String: ");
Scanner sc=new Scanner(System.in);
String str=sc.nextLine();
System.out.println("Enter The Character to find: ");
char ch=sc.next().charAt(0);
position=str.indexOf(ch);
if(position>0)
System.out.println("The position of "+ ch +" is "+ (position 1));
else
System.out.println(ch +" is not Present in the Given string");
}
}
Language:

Monika
11 Jun, 2017 1:43 PM
<!DOCTYPE html>
<html>
<body>
<?php
function Search_char() {
// input string
$str ="what is your name";
echo "Input string is :" . $str."<br/>";
// conversion of string into array
$arr = str_split($str);
// counting lengh of array
$arrlength=count ($arr);
// char to be search
$ch="a"; $pos=0;
echo "Element to be search is : a <br/>";
// loop for reading the input array
for($x=1; $x<=$arrlength; $x++)
{
if( $arr[$x]==$ch)
{
$pos= $x+1;
echo "<br/>Element found at :".$pos ;
}
}
if($pos==0)
{
echo "no element found";
}
}
// call the function
Search_char();
?>
</body>
</html>
Language:

Jyoti Choudhary
17 Jun, 2017 11:40 PM
import java.util.*;
import java.io.*;
import java.lang.*;
class SearchChar
{
public static void main (String[] args)
{
int position=0;
System.out.println("Enter The String: ");
Scanner sc=new Scanner(System.in);
String str=sc.nextLine();
System.out.println("Enter The Character to find: ");
char ch=sc.next().charAt(0);
position=str.indexOf(ch);
if(position>0)
System.out.println("The position of" + ch + " is " + (position 1));
else
System.out.println(ch +" is not Present in the Given string");
}
}
Language:

Amit
7 Jul, 2017 9:30 AM
#include <stdio.h>
#include <string.h>
int main()
{
char a, word[50];
int i, freq = 0, flag = 0;
printf("Enter character: ");
scanf("%c", &a);
printf("Now enter the word: ");
scanf("%s", word);
printf("Positions of '%c' in %s are: ", a, word);
for (i = 0; i < strlen(word); i++)
{
if (word[i] == a)
{
flag = 1;
printf("%d ", i + 1);
freq++;
}
}
if (flag)
{
printf("\nCharacter '%c' occured for %d times.\n", a, freq);
}
else
{
printf("None\n");
}
return 0;
}
Language:
REPLY

Velupucharla
3 Feb, 2021 11:54 AM
Outout
Language:

Neha
7 Jul, 2017 9:30 AM
#include <stdio.h>
#include <string.h>
#include<iostream>
using namespace std;
int main()
{
char a, word[50];
int i, freq = 0, flag = 0;
printf("Enter character: ");
scanf("%c", &a);
printf("Now enter the word: ");
scanf("%s", word);
printf("Positions of '%c' in %s are: ", a, word);
for (i = 0; i < strlen(word); i++)
{
if (word[i] == a)
{
flag = 1;
printf("%d ", i + 1);
freq++;
}
}
if (flag)
{
printf("\nCharacter '%c' occured for %d times.\n", a, freq);
}
else
{
printf("None\n");
}
return 0;
}
int main()
{
char a, word[50];
int i, freq = 0, flag = 0;
printf("Enter character: ");
scanf("%c", &a);
printf("Now enter the word: ");
scanf("%s", word);
printf("Positions of '%c' in %s are: ", a, word);
for (i = 0; i < strlen(word); i++)
{
if (word[i] == a)
{
flag = 1;
printf("%d ", i + 1);
freq++;
}
}
if (flag)
{
printf("\nCharacter '%c' occured for %d times.\n", a, freq);
}
else
{
printf("None\n");
}
return 0;
}
Language:

Rajeev Ranajn
16 Aug, 2017 8:16 AM
package stringprogram;
import java.util.Scanner;
public class StringIndexFIndTraverse
{
public static void main(String[] args)
{
String a;
int i;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the String : ");
String str = sc.nextLine();
System.out.println("Enter the Character which you want to search : ");
String ch = sc.next();
String s[] = str.split("");
for(i=0;i<s.length;i )
{
if(s[i].equalsIgnoreCase(ch))
{
System.out.println(ch " is at " (i-1));
}
}
}
}
Language:

Veera Chowdary Kamani
5 Sep, 2018 1:10 PM
st="what is your name?";count=0
for i in st:
count=count 1
if(i=="a"):
print("The Position Of a is " str(count))
break
Language:

Baig
3 Jun, 2020 12:37 PM
<pre style="background-color:#2b2b2b;color:#a9b7c6;font-family:'JetBrains Mono';font-size:9.8pt;"><span style="color:#cc7832;">import </span>java.util.Scanner<span style="color:#cc7832;">;<br></span><span style="color:#cc7832;"><br></span><span style="color:#cc7832;">public class </span>printthedesiredcharacterfromastring {<br> <span style="color:#cc7832;">public static void </span><span style="color:#ffc66d;">main</span>(String[] args) {<br> Scanner sc = <span style="color:#cc7832;">new </span>Scanner(System.<span style="color:#9876aa;font-style:italic;">in</span>)<span style="color:#cc7832;">;<br></span><span style="color:#cc7832;"> </span>String s = sc.nextLine()<span style="color:#cc7832;">;<br></span><span style="color:#cc7832;"> </span>String c = sc.next()<span style="color:#cc7832;">;<br></span><span style="color:#cc7832;"> int </span>d=<span style="color:#6897bb;">0</span><span style="color:#cc7832;">;<br></span><span style="color:#cc7832;"> for </span>(<span style="color:#cc7832;">int </span>i = <span style="color:#6897bb;">0</span><span style="color:#cc7832;">; </span>i
Language:

Kumari
10 Jan, 2022 9:19 AM
//Write a program to print the desired character from a string or search a char in a string.
import java.util.*;
public class Practice5
{
public static void main(String[] args)
{
Scanner s = new Scanner(System.in);
String st = s.nextLine();
char c = s.next().charAt(0);
char ch[] = st.toCharArray();
for(int i=0;i<ch.length;i )
{
if(ch[i]==c) {
System.out.println(c);
break;
}
}
}
}
Language:

Kumari
10 Jan, 2022 9:19 AM
//Write a program to print the desired character from a string or search a char in a string.
import java.util.*;
public class Practice5
{
public static void main(String[] args)
{
Scanner s = new Scanner(System.in);
String st = s.nextLine();
char c = s.next().charAt(0);
char ch[] = st.toCharArray();
for(int i=0;i<ch.length;i )
{
if(ch[i]==c) {
System.out.println(c);
break;
}
}
}
}
Language:

Chavan
26 Jan, 2023 5:57 PM
import java.util.Scanner;
//Write a program to print the desired character from a string or search a char in a string.
public class SearchCharacter {
public static void main(String args[]){
Scanner sc=new Scanner((System.in));
String str=sc.nextLine();
String s[]=str.split("");
String c="a";
for(int i=0;i<str.length();i ){
if(s[i].equalsIgnoreCase(c)){
System.out.println("char " c "Found At Index" (i 1));
}
}
}
}