Find Occurrence Of Each Word In a Sentence

Description

To print occurrence of words in sentence
take a variable count and traverse the sentence and if a word occure
again increment the count.
Example: Input : this is java and java is good
         Output: this 1
                 is   2
                 java 2
                 and  1
                 good 1

C/C++

/* C program to count number of occurance of every words in sentence*/
//Save it as PrintOccurenceOfWordsSentence.c

#include<stdio.h>
int main(){

    int i, j=0, k=0;

    char str[100];
    char newStr[10][20];

    printf("Enter a string : ");
    scanf("%[^\n]s", str);
    
    //Traversing the string
    for(i=0;str[i]!='\0';i++){
        
        //checking for space
        if(str[i]==' '){
            newStr[k][j]='\0';
            k++;
            j=0;
        }else{
            newStr[k][j]=str[i];
            j++;
        }
    }
    newStr[k][j]='\0';

    for(i=0;i<=k;i++){
        int countval=0;
        for(j=0;j<=k;j++){
            
            //Storing the string value into temporary variable
            char *temp1 = newStr[i];
            char *temp2 = newStr[j];
            
            /*if second loop encounter same value again but j<i break the loop to
            avoid printing of duplicate value*/
            if(j<i && (strcmp(temp1,temp2) == 0)) {
                break;
            }
            
            //if second loop encounter same value increase the value of count
            if(strcmp(temp1,temp2)==0) {
                countval++;
            }
            
            /*While traversing the array, if loop reached to last print the
            string with its number of count*/
            if(j==k) {
                printf("%s %d\n",newStr[i],countval);
            }
        }
    }

    return 0;
}
Input:
Enter a string : 
this is java and java is good

Output:
this 1
is 2
java 2
and 1
good 1

Java

/* Java program to count number of occurance of every words in sentence*/
//Save it as PrintOccurenceOfWordsSentence.java

import java.io.*;
import java.util.Scanner;

public class PrintOccurenceOfWordsSentence {

    public static void main(String[] args) {
        
        Scanner scanner = new Scanner(System.in);
        
        System.out.println("Enter a string : ");
        String str = scanner.nextLine();
        
        //Splitting the string by space and storing into string array
        String strArray[] = str.split(" ");
        
        for(int i=0;i<strArray.length;i++) {
            int count=0;
            for(int j=0;j<strArray.length;j++) {
                
                //Storing the string value into temporary variable
                String temp1 = strArray[i];
                String temp2 = strArray[j];
                
                /*if second loop encounter same value again but j<i break the loop to 
                avoid printing of duplicate value*/
                if(j<i && temp1.equals(temp2)) {
                    break;
                }
                
                //if second loop encounter same value increase the value of count
                if(temp1.equals(temp2)) {
                    count++;
                }
                
                /*While traversing the array, if loop reached to last print the
                string with its number of count*/
                if(j==strArray.length-1) {
                    System.out.println(strArray[i]+" "+count);
                }
            }
        }
    }
}
Input:
Enter a string : 
this is java and java is good

Output:
this 1
is 2
java 2
and 1
good 1

Related Programs

1) Count Number of Words in a String
2) Print All Unique Words Of A String
3) Program To Print Unique Word Of A String Using Set
4) Program to Replace Spaces With Dots
5) Program to copy string
6) Program to Compare Two Strings
7) Program to Reverse a String
8) Find Substring of a Given String
9) Program to Insert a String into Another String
10) How to find Length or Size of a String
11) Program to sort characters of strings in alphabetical order
12) Program to Sort set of strings in alphabetical order
Share Me

Leave a Reply