Count Common Words in Two Strings

Count+Identify common words in two string vectors [R]

You can split string at each word and perform the operation.

In base R :

numwords <- function(str1, str2) {
mapply(function(x, y) length(intersect(x, y)),
strsplit(str1, ' '), strsplit(str2, ' '))
}

matchwords <- function(str1, str2) {
mapply(function(x, y) paste0(intersect(x, y),collapse = " "),
strsplit(str1, ' '), strsplit(str2, ' '))
}

numwords(strvec1, strvec2)
#[1] 2 0 2 3 4

matchwords(strvec1, strvec2)
#[1] "Griffin Morgan" "" "Landon Lark"
#[4] "Megan Thrall Michels" "Tyler Westbrook Grayson Didas"

How to count mutual words in 2 strings in Java?

As jackarms said, you are using == to compare strings where you should be using .equals. See this.

Try

  int num = 0;
String[] a = s1.split(" ");
String[] b = s2.split(" ");

for (int i = 0; i < a.length; i++) {
for (int j = 0; j < b.length; j++) {
if (a[i].equals(b[j])) {
num++;
}
}
}

return num;

Finding common words in given two strings

I think with this exercise you supposed to learn these things:

  1. do not use magic numbers, use #define instead
  2. always check pointers for NULL if function can return NULL
  3. check for boundaries, make sure your code behaves reasonable if input is too long
  4. do not forget free memory you allocated
  5. use library functions when possible

Here an example how you it can be done.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_WORD_LEN 30
#define MAX_WORDS 10

char ** commonWords(char *str1, char *str2)
{
if (str1 != NULL && str2 != NULL)
{
char a[MAX_WORD_LEN+1];
char **res = (char**)calloc(MAX_WORDS+1, sizeof(char*));
if(!res) return NULL;

int k = 0, j = 0;
for (int i = 0; str1[i] != '\0' && k < MAX_WORDS; i++)
{
if (str1[i] != ' ' && j < MAX_WORD_LEN)
a[j++] = str1[i];
if (str1[i] == ' ' || str1[i] == '\0') {
a[j] = '\0';
if (strstr(str2,a)) {
res[k++] = strdup(a);
}
j = 0;
}
}
return res;
}
return NULL;
}

int main(void)
{
char **cw = commonWords("one two three", "two three five");
if(cw) {
for(int i=0; cw[i]; i++) {
printf("%s ",cw[i]);
free(cw[i]);
}
free(cw);
}
printf("\n");
return 0;
}

Please remember that you can learn only if you practice ;)

How to count common characters in two strings in JavaScript?

We can convert the second input string to an array, then the next step is to iterate over the first input string and find a match in the second input string's character array.

If a match is found, increment the counter and remove that character from the second input string's character array so that it is not considered in the next match:

//Solution:function getSameCount(str1, str2) {  let count = 0;  const obj = str2.split("");  for(str of str1){    let idx = obj.findIndex(s => s === str);    if(idx >= 0){      count++;      obj.splice(idx, 1);    }  }  return count;}
//Test:console.log(getSameCount("abcd", "aad"));console.log(getSameCount("geeksforgeeks", "platformforgeeks"));console.log(getSameCount("aad", "abcd"));console.log(getSameCount("platformforgeeks", "geeksforgeeks"));

Find the number of words that occur in both strings

You can get the same words by doing this: list(set(a.split(' ')) & set(b.split(' ')))

Thanks to this answer.



Related Topics



Leave a reply



Submit