Program Discussion :: Strings
55 / 60
Write a program to enter the sentence then find the word from the sentence.
Answer:
Asked In ::
Language:
Monika Jain
20 Jun, 2017 10:39 PM
<!DOCTYPE html>
<html>
<body>
<?php
function String_word() {
$str ="what is your name"; /* input string */
echo "Input string is :" . $str."<br/>";
$arr = explode( " ", $str); /* conversion of string into array */
$arrlength=count ($arr); /* counting lengh of array */
$word="your"; /* initialising an empty array and its initial index */
for( $x =0;$x<=$arrlength; $x++) /* loop for reading the input array */
{
if($arr[$x]==$word)
{ $pos=$x;
break;
}
}
echo $word."- word found at position: ".++$pos; // printing string in reverse order
}
String_word();
?>
</body>
</html>
Language:
Siddhartha Paul
19 Jun, 2019 2:16 PM
import java.util.*;
public class FindWord
{
public static void main(String[] args)
{
int count=0;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the sentence: ");
String line=sc.nextLine();
System.out.println("Enter the word to be searched: ");
String word=sc.nextLine();
String s="";
for(char ch : line.toCharArray())
{
s=s+ch;
if(s.length()==word.length())
{
if(s.equals(word))
count++;
s=s.substring(1);
}
}
if(count>0)
System.out.println("word is found and the count of the given word is: "+count);
else
System.out.println("word not found");
}
}