Program Discussion :: Basics
5. Write a program to print the desired character from a string or search a char in a string.
5. Write a program to print the desired character from a string or search a char in a string.
Answer:
#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;
}
Asked In :: IBM
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:

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
<p>
package stringprogram;</p>
<p>import java.util.Scanner;</p>
<p>public class StringIndexFIndTraverse
{</p>
<p> 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));
}
}
}
}</p>
Language:

Veera Chowdary Kamani
5 Sep, 2018 1:10 PM
<p>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</p>