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

  • Home
  • SEARCH
  • 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 6734339
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:52:37+00:00 2026-05-26T10:52:37+00:00

If I had an array of characters for example: A = [w, o, r,

  • 0

If I had an array of characters for example:

A = [w, o, r, n, g, , w, o, r, d]

And another array for example:

B = [c, o, r, r, e, c, t, , w, o, r, d, .]

I need to compare the words in array A (which are separated by a blank space) to array B and if any of the words in the first array exist in the second array, then that word should be printed. So for example, since “word” exists in the first array and in the second array, then “word” should be printed out.

How do I go about 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-26T10:52:37+00:00Added an answer on May 26, 2026 at 10:52 am

    Let’s see how I would do it:

    You will need a function that, given an array of char, splits it in an array of words (and put them in C strings, NUL terminated please 🙂 ). I would put the length of this array and the array in a struct

    struct WordCollection
    {
        size_t NumWords;
        char **Words;
    }
    

    Now… How to do this function?

    Let’s say we “cheat” a little and decide that our arrays A and B are NUL terminated (or if they are . terminated like B, then you replace the . with a NUL). Now, this being C, you should first count the number of spaces in the string, allocate an array of char* (WordCollection::Words) big enough to contain n + 1 char* (and put this n + 1 in WordCollection::NumWords) and using strtok “tokenize” the string and put the words in the array you created.

    Then you should (could) split the A and B array in words using this function. You’ll obtain two WordCollection, A1 and B1.

    To make it quicker, I would qsort B1.

    Then for each word in A1 you bsearch it in B1 (it isn’t a bad word… It means Binary Search, and it’s a quick method of searching something in an ordered array)

    Done 🙂

    I’ll add that, if this is the first time you use bsearch and qsort, it’s better you look at the samples you can find around. Their syntax can be “tricky”.

    Now… I know you won’t look at the code 🙂 so I’ll put it here

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    struct WordCollection
    {
        size_t NumWords;
        char **Words;
    };
    
    void splitWord(char *str, struct WordCollection *wc)
    {
        char *c;
        char **currentWord;
    
        c = str;
    
        wc->NumWords = 1;
    
        while (*c != '.')
        {
            if (*c == ' ')
            {
                wc->NumWords++;
            }
    
            c++;
        }
    
        *c = '\0';
    
        wc->Words = (char**)malloc(wc->NumWords * sizeof(char*));
    
        c = strtok(str, " ");
    
        currentWord = wc->Words;
    
        while (c)
        {
            *currentWord = c;
            currentWord++;
    
            c = strtok(NULL, " ");
        }
    }
    
    int myComp(const void *p1, const void *p2)
    {
        return strcmp(*(const char**)p1, *(const char**)p2);
    }
    
    int main(void)
    {
        char a[] = { 'w', 'o', 'r', 'n', 'g', ' ', 'w', 'o', 'r', 'd', '.' };
        char b[] = { 'c', 'o', 'r', 'r', 'e', 'c', 't', ' ', 'w', 'o', 'r', 'd', '.' };
    
        struct WordCollection a1, b1;
        struct WordCollection *pSmaller, *pBigger;
    
        size_t i;
    
        splitWord(a, &a1);
        splitWord(b, &b1);
    
        if (a1.NumWords <= b1.NumWords)
        {
            pSmaller = &a1;
            pBigger = &b1;
        }
        else
        {
            pSmaller = &b1;
            pBigger = &a1;
        }
    
        qsort(pBigger->Words, pBigger->NumWords, sizeof(char*), myComp);
    
        for (i = 0; i < pSmaller->NumWords; i++)
        {
            void *res = bsearch(&pSmaller->Words[i], pBigger->Words, pBigger->NumWords, sizeof(char*), myComp);
            if (res)
            {
                printf("Found: %s", pSmaller->Words[i]);
            }
        }
    
        free(a1.Words);
        free(b1.Words);
    
        return 0;
    }
    

    And on ideone

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

Sidebar

Related Questions

If for example you had an associative array which looked something like this: $array
I was wondering if anyone had an example of what an array would look
If, for example, I had array like this: array(34) { [ahostel.lt/img/background.png]=> array(4) { [name]=>
I originally had an array[1..1000] that was defined as a global variable. But now
If you had an array of Strings, what is the quickest way to sort
i had a question in my program. When I pass the 3D int array
Just need to see if a paragraph contains a stop word, the stop words
How do you count duplicates in a ruby array? For example, if my array
I need to get a ruby array in to a javascript array and I'm
I really wish Processing had push and pop methods for working with Arrays, but

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.