Tutorials

Find Duplicate or Repeating Words in String Using C

main.c

#include <stdio.h>  
#include <string.h>  
   
int main()  
{     
    char string[] = "Small brown bug bit a small brown dog on his small brown nose";  
    char words[100][100];  
    int i = 0, j = 0, k, length, count;  
      
    //Split the string into words such that each row of array words represents a word  
    for(k=0; string[k]!='\0'; k++){  
          
        //Here, i represents row and j represents column of two-dimensional array words  
        if(string[k] != ' ' && string[k] != '\0'){  
            //Converts the string into lowercase and add it to array words  
            words[i][j++] = tolower(string[k]);  
        }  
        else{  
            words[i][j] = '\0';  
            //Increment row count to store new word  
            i++;  
            //Set column count to 0  
            j = 0;  
        }  
    }  
      
    //Store row count in variable length  
    length = i+1;  
      
    printf("Duplicate words in the given string: \n");  
    for(i = 0; i < length; i++){  
        count = 1;  
        for(j = i+1; j < length; j++){  
           if(strcmp(words[i], words[j]) == 0 && (strcmp(words[j],"0") != 0)){  
               count++;  
               //Set words[j] to 0 to avoid printing visited word  
               strcpy(words[j],"0");  
           }   
        }  
        //Displays the duplicate word if count is greater than 1  
        if(count > 1 )  
            printf("%s\n", words[i]);  
    }  
    
    return 0;  
}
Furqan

Well. I've been working for the past three years as a web designer and developer. I have successfully created websites for small to medium sized companies as part of my freelance career. During that time I've also completed my bachelor's in Information Technology.

Recent Posts

MiniMax-M1 vs GPT-4o vs Claude 3 Opus vs LLaMA 3 Benchmarks

MiniMax-M1 is a new open-weight large language model (456 B parameters, ~46 B active) built with hybrid…

June 22, 2025

How to Use Husky with npm to Manage Git Hooks

Managing Git hooks manually can quickly become tedious and error-prone—especially in fast-moving JavaScript or Node.js…

June 22, 2025

How to Use Lefthook with npm to Manage Git Hooks

Git hooks help teams enforce code quality by automating checks at key stages like commits…

June 22, 2025

Lefthook vs Husky: Which Git Hooks Tool is Better? [2025]

Choosing the right Git hooks manager directly impacts code quality, developer experience, and CI/CD performance.…

June 22, 2025

Llama 3.1 vs GPT-4 Benchmarks

We evaluated the performance of Llama 3.1 vs GPT-4 models on over 150 benchmark datasets…

July 24, 2024

Transforming Manufacturing with Industrial IoT Solutions and Machine Learning

The manufacturing industry is undergoing a significant transformation with the advent of Industrial IoT Solutions.…

July 6, 2024