Program Discussion :: Strings
58 / 60
Write a program to print a desired character from string
Answer:
#include
#include
#include
using namespace std;
void main()
{
int i, j, k, count = 0;
char str[50];
coutstr;
k = strlen(str);
cout
Asked In ::
Language:
Monika Jain
20 Jun, 2017 10:39 PM
<!DOCTYPE html>
<html>
<body>
<?php
function Search_char() {
$str ="what is your name"; /* input string */
echo "Input string is :" . $str."<br/>";
$arr = str_split($str); /* conversion of string into array */
$arrlength=count ($arr); /* counting lengh of array */
$ch="a"; $pos=0; /* char to be search */
echo "Element to be search is : a <br/>";
for($x=1; $x<=$arrlength; $x++) /* loop for reading the input array */
{
if( $arr[$x]==$ch)
{
$pos= $x+1;
break;
}
}
if($pos==0)
{
echo "no element found";
}
else
{
echo "Element found at :".$pos ;
}
}
Search_char();
?>
</body>
</html>
Language:
Shyam
7 Jul, 2017 9:30 AM
#include <stdio.h>
#include <string.h>
void main()
{
int i, j, k, count = 0;
char str[50];
printf("enter string\n");
scanf("%[^\n]s", str);
k = strlen(str);
printf("desired characters in a string is\n");
for (i = 2;i <= k;i++)
{
printf("%c\n", str[i - 1]);
}
}
Language:
Nick
7 Jul, 2017 9:30 AM
#include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std;
void main()
{
int i, j, k, count = 0;
char str[50];
cout<<"enter string\n";
cin>>str;
k = strlen(str);
cout<<"desired characters in a string is\n";
for (i = 2;i <= k;i++)
{
cout<<str[i - 1];
}
}
Language:
Jaykumar
25 Aug, 2017 12:42 AM
import java.util.*;
import java.lang.*;
import java.io.*;
class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
int position=0;
System.out.println("Enter the String");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String str=br.readLine();
System.out.println("Enter the character to find");
char ch=br.readLine().charAt(0);
position=str.indexOf(ch);
System.out.println("The position of "+ch+" is "+(position+1));
}
}
Language:
Siddhartha Paul
19 Jun, 2019 2:16 PM
import java.util.*;
public class SearchNPrint
{
public static void main(String []args)
{
int found=0,i;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the string: ");
String str=sc.nextLine();
System.out.println("Enter the character to find: ");
char c=sc.nextLine().charAt(0);
int len=str.length();
for(i=0;i<len;i++)
{
char ch=str.charAt(i);
if(ch==c)
{
found=1;
break;
}
else
continue;
}
if(found==1)
System.out.println(c+" found at position "+(i+1));
else
System.out.println(c+" not found");
}
}