Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4041904
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T12:56:19+00:00 2026-05-20T12:56:19+00:00

I am solving a problem in C where i have to find duplicate words

  • 0

I am solving a problem in C where i have to find duplicate words in astring like

 char a[]="This is it This";

In above string “This” appears two times so I would like to count it as one.

Can anybody suggest how to achieve this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T12:56:20+00:00Added an answer on May 20, 2026 at 12:56 pm

    Here is a program that does what you’re asking. It is hard coded for 4 words of a max 99 characters. That can be changed easily; I just fit it around your input. I also used strcmp and strcpy. Both of these functions can be implemented on your own (call them mystrcpy and mystrcmp and embed them). I’m not rewriting the string functions for you. I did show how to avoid strtok based on the other answer. I looked them up and they are not complex, but they did not add anything to the program and I didn’t want to reinvent the wheel. Last of all, I just used a simple linear search in the notInArray function. For a large data set this is not efficient (you would probably use some type of tree or hash).

    This was compiled under gcc version 4.3.4

    #include <stdio.h>
    #include <string.h>
    
    int notInArray(char arr[][100], char *word, int size);
    
    int main() {
      char a[] = "This is a This";
      char *ptr;
      char strarr[4][100];
      char word[100];
      int pos = 0;
      int count = 0;
      int i;
    
      memset(&strarr,0,sizeof(strarr));
      printf("%s\n\n",a);
    
      ptr = a;
      while (*ptr) {
    
        sscanf(ptr, "%s ", word);
        if (notInArray(strarr,word,4)) {
          strcpy(strarr[pos++],word);
          printf("%s\n", word);
        }
    
        while (!isspace(*ptr++) && *ptr) {}
      }
    
      for (i=0; i<4; i++) {
        if (*strarr[i]) {
          printf("strarr[%d]=%s\n",i, strarr[i]);
          count++;
        }
      }
    
      printf("\nUnique wordcount = %d\n", count);
    
      return(0);
    }
    
    int notInArray(char arr[][100], char *word, int size) {
      int i;
    
      for (i=0; i<size; i++) {
        if (*arr[i] && !strcmp(arr[i],word)) {
          return(0);
        }
      }
    
      return(1);
    }
    

    The output looks like:

    ~>a
    This is a This
    
    This
    is
    a
    strarr[0]=This
    strarr[1]=is
    strarr[2]=a
    
    Unique wordcount = 3
    

    Enjoy.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.