c - 函数和字符串,检查输入字符串是否匹配

标签 c arrays string function char

我想创建一个名为 LetterDifference(char *wordA, char *wordB) 的函数

我希望该函数扫描两个单词,如果单词之间有一个字母差异,则返回 1。例如,如果

wordA = cool

wordB = tool

注:WordAWordB总是相同的长度

我希望函数返回 1

这是我尝试过的,我的代码无法编译,我认为我的理解有错误。有人可以帮我吗?

int LetterDifference(char *wordA, char *wordB)
{
    int i;
    int count = strlen(word1);
    while (i < strlen(wordA)) {
        if (wordA[i] == wordB[i]) {
            count = count - 1;
            i++;
        }
    }
    if (count == 1) {
        return 1;
    }
}

最佳答案

这个@new 编码器背后的逻辑非常简单。

我想首先指出一些需要纠正的错误。

  1. 如果 wordAwordB 长度不等,控件就会越界。在继续之前,您需要确保两个单词的长度相等。
  2. word1(您用于初始化 count 变量的长度)是一个不存在的变量。它应该是 wordA
  3. 对于 count 结果不为 1 的情况,没有返回值。

解决方案:

您只需从 0 循环到任意一个字符串的长度。检查每个索引处两个字符串中相应的元素/字符,对于每个不相等的字符,递增 count 变量。如果counter最终等于1。这意味着有一个字母的差异。

我已经发布了下面的代码。它还确保用户仅输入相同长度的字符串。希望对您有所帮助。

代码:

#include<stdio.h>
#include<string.h>

int LetterDifference(char[] , char[] );

int main() {
  int l1, l2, flag = 0, res;
  char a[10], b[10];
  while(flag==0) {
    printf("\nEnter 2 words each of the same length:");
    printf("\nenter the 1st word: ");
    scanf("%s",&a);
    printf("\nenter the 2nd word: ");
    scanf("%s",&b);
    l1 = strlen(a);
    l2 = strlen(b);
    if(l1==l2) {
      flag = 1;
    }
    else {
      printf("\nlengths of the words are unequal...enter again!!!");
      flag = 0;
    }
  }
  printf("\n%s\n%s",a,b);
  res = LetterDifference(a, b);
  if(res==1) {
    printf("\nThere is a 1-letter difference.");
  }
  else {
    printf("\nNO one letter difference.");
  }
  return 0;
}

int LetterDifference(char wordA[], char wordB[])
{
    int i, count = 0;
    int j,c[strlen(wordA)];
    //use the following for loop if you are looking to match corresponding characters from each word
    //for(i = 0; i < strlen(wordA); i++) {
    //  if(wordA[i]!=wordB[i]) {
    //    count++;
    //  }
    //}
    //use the following for loop if you are looking to match any characters from word regardless of the order in which they appear
    for(i = 0; i < strlen(wordA); i++) {
      c[i]=0;
      for(j = 0; j < strlen(wordB); j++) {
        if(wordA[i]==wordB[j]) {
          c[i]++;
        }
      }
      if(c[i]==0) {
        count++;
      }
    }
    if (count == 1) {
        return 1;
    }
    return 0;
}

输出: 案例 1:考虑词序

Enter 2 words each of the same length:

enter the 1st word: abcd

enter the 2nd word: abcz

abcd

abcz

There is a 1-letter difference.

另一个输入

Enter 2 words each of the same length:

enter the 1st word: qqqq

enter the 2nd word: q

lengths of the words are unequal...enter again!!!

Enter 2 words each of the same length:

Enter 2 words each of the same length:

enter the 1st word: qqqq

enter the 2nd word: aaaa

qqqq

aaaa

NO one letter difference.

情况 2:无论字符顺序如何

Enter 2 words each of the same length:

enter the 1st word: qwewew

enter the 2nd word: ewwewf

qwewew

ewwewf

There is a 1-letter difference.

关于c - 函数和字符串,检查输入字符串是否匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52717560/

相关文章:

c - 如果我的数字变量超过特定值,则 if 和 else 语句不起作用

java - 二维数组表示

java - ParseJson.java,如果没有数组名称则解析结果?

c++ - 如何使用 ZMQ 发送字符串结构

string - 如何在 Rust 中惯用且高效地解析由字母和数字组成的字符串?

c - 将时间间隔结构化为可打印格式

c - 在 C 中处理 SIGTSTP 信号

Javascript : What is the best way to detect combination of two keys in array of an object

javascript - Angular2 从对象数组中获取值

c - 使用 scanf() 从键盘读取时从先前输入读取换行符