Labs247 Interview Question
Quality Assurance EngineersCountry: India
Interview Type: In-Person
#include <iostream>
#include <string>
#include <map>
using namespace std;
void countWordsInString(string a)
{
if(a.empty())return;
int low = 0;
int high = 0;
map<string, int> countCenter;
while(a[low] != '\0' && a[low] == ' ')
{
low++;
high = low;
}
while(high < (int)a.size())
{
while(a[high] != ' ' && a[high] != '\0')
high++;
countCenter[a.substr(low, high - low)]++;
while(a[high] == ' ')
high++;
low = high;
}
for(map<string, int>::iterator i = countCenter.begin(); i != countCenter.end(); i++)
{
cout<<i->first<<":"<<i->second<<endl;
}
}
int main()
{
string a = "Hello I am going to I with Hello am";
countWordsInString(a);
}
As the questions speaks about finding the count of a particular word.
So, a buffer can be maintained for each word & matched with the word to be searched.
An alternate way is to use TRIE & at the leaf nodes, count the number of occurrences. If words are to be outputted in sequence with the sentence, search for each word in the TRIE else do an inorder traversal.
/* find the no. of time a particular word is occurring in a string?
eg. "Hello I am going to I with Hello am"? */
// WORKING CODE
#include<stdio.h>
#include<string.h>
int main()
{
char S[50],W[20][10],C[20];
int i=0,j,l,k=0,A[20]={0},flage=0;
gets(S);
l=strlen(S);
W[0][0]='\0';
while(i<l)
{
j=0;
flage=0;
for(;S[i]!=' ' && i<l;i++)
C[j++]=S[i];
C[j]=NULL;
for(j=1;j<=k;j++)
if(!strcasecmp(C,W[j])) // strcasecmp() return 0 on equal
{
A[j]++;
flage=1;
break;
}
if(!flage)
{
k++;
strcpy(W[k],C);
A[k]++;
}
i++;
}// end of while
printf("\n");
for(i=1;i<=k;i++)
printf("\n%d\t%s",A[i],W[i]);
printf("\n");
return 0;
}
public static void main(String[] args) {
String word="Hello I am going to I with Hello am";
HashMap<String,Integer>map=new HashMap<String,Integer>();
String []words=word.split(" ");
for(String wor:words){
if(map.containsKey(wor)){
int count=map.get(wor);
count=count+1;
map.put(wor, count);
}
else{
map.put(wor, 1);
}
}
System.out.println(map);
}
}
public static int countOccurance(String input, String word, int count) {
if (input == null || word == null || input.isEmpty() || word.isEmpty())
return 0;
if (input.length() < word.length())
return count;
String subString = input.substring(0, word.length());
if (subString.equals(word))
return countOccurance(input.substring(word.length()), word, count + 1);
else
return countOccurance(input.substring(1), word, count);
}
// Find the number of occurrences of a particular word.
public static int countOccurance(String input, String word, int count) {
if (input == null || word == null || input.isEmpty() || word.isEmpty())
return count;
if (input.length() < word.length())
return count;
String subString = input.substring(0, word.length());
if (subString.equals(word))
return countOccurance(input.substring(word.length()), word, count + 1);
else
return countOccurance(input.substring(1), word, count);
}
In java it can be done like this:
String str ="Hello I am going to I with Hello am";
String[] arr_str = str.split(" ");
int len =arr_str.length;
//sort the string
for(int i=0; i<len; i++)
{
for(int j=1; j<(len-i); j++)
{
if(arr_str[j-1].compareToIgnoreCase(arr_str[j])>0)
{
String temp = arr_str[j-1];
arr_str[j-1]=arr_str[j];
arr_str[j]=temp;
}
}
}
//now count the number of times the words are repeated
try{
for(int a=0; a<len; a++)
{
int count =1;
if(arr_str[a].equals(arr_str[a+1]))
{
count++;
System.out.println(arr_str[a]+" is repeated "+count+" times" );
}
}
}
catch(Exception e){}
}
public static void main(String args[]){
String sat = "Hello I am going to I with Hello am";
String[] s = sat.split(" ");
for(int i=0;i<s.length;i++){
int count = 0;
for(int j=0;j<s.length;j++){
if(s[i].equals(s[j])){
count = count+1;
// System.out.print(s[j] + " ");
}
}
System.out.println(s[i]+" "+count);
}
}
ublic class NumberOfOccurringWord {
public static void main(String args[]){
String name = "hello i am going to hello";
String word = "hello";
int i;
int count = 0;
String arr[] = name.split(" ");
for(i=0 ;i<arr.length;i++){
if(word.equals(arr[i])){
++count;
}
}
System.out.print(count);
}
public static void occuranceWord(String string, String word){
- naveengarg2k5 July 23, 2012String original = string;
int count =0;
String[] str2 = original.split(" ");
int l = str2.length;
String str3=null;
for(int i=0; i<l; i++){
if(word.equals(str2[i])){
++count;
}
}
System.out.println(count);
}