我想比较两个字符串,删除一个字符后是否完全匹配。
(从位置0开始索引)
例如
设置1:橙色
字符串2:年龄
输出应为:
The string matches at position 2, after deleting position 3.
我该如何解决这个问题?
这是我的代码:
#include <stdio.h>
#include <string.h>
void search(char* pat, char* txt)
{
int M = strlen(pat);
int N = strlen(txt);
int j;
int position;
if (M > N)
{
printf("No Excat Matching");
}
for (int i = 0; i <= N - M; i++)
{
for (j = 0; j < M; j++)
{
if (txt[i + j] != pat[j]&&txt[i + j +1] != pat[j])
{
break;
}
}
if (j == M )
printf("The target string matches staring at position %d after deleting the
character at position %d\n",i,position);
}
}
int main()
{
char txt[100];
char pat[100];
printf("Enter some text\n");
gets(txt);
printf("Enter a string to find\n");
gets(pat);
search(pat, txt);
return 0;
}
答案 0 :(得分:0)
首先,请尝试对字段和方法使用好名字,以便将来为其他人和您自己阅读代码。
在每个示例中,您可以命名两个参数comparisonString
和searchedString
。
关于您的代码,长度比较不错。
对于第二部分,我将尝试仅使用一个for
。在其中迭代比较字符串。
为此,您应该首先尝试找到您的第一个字符。
找到此字符后,您需要检查其他字符是否也包含在字符串中。
如果不保存第一个不匹配的字符,并且当您有两个不匹配的字符时,立即停止迭代并返回该函数。如果需要,请先打印错误消息。
int numChar = 0;
int numUnmatchingChar = 0;
int firstMatchPos = -1;
int unmatchingCharPos = -1;
for(int i = 0; i < comparisonString; i++)
{
if(comparisonString[i] == searchedString[numChar])
{
if(numChar + 1 < M)
{
numChar++;
}
else
{
break;
}
if(firstMatchPos == -1)
{
firstMatchPos = i;
}
}
else if(firstMatchPos != -1)
{
if(unmatchingCharPos == -1)
{
unmatchingCharPos = i;
}
else
{
firstMatchPos = -1;
numChar = 0;
unmatchingCharPos = -1;
}
}
}
if(firstMatchPos != -1)
{
if(unmatchingCharPos != -1)
{
printf("The target string matches staring at position %d after deleting the
character at position %d\n",firstMatchPos,unmatchingCharPos);
}
else
{
printf("The target string matches staring at position %d);
}
}
这应该可以解决问题。但是您只能找到一种情况。
答案 1 :(得分:0)
发现这个问题带有C标签后,我重新回答了。
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <stdbool.h>
bool foundWord(char *stringToLookIn, char *stringToLookFor, int *matchingPosition)
{
char *found = strstr(stringToLookIn, stringToLookFor);
if(found != NULL)
{
*matchingPosition = found;
return true;
}
else
{
return false;
}
}
int main()
{
char stringToLookIn[100];
char stringToFind[100];
uint32_t matchingPosition;
printf("First string \n");
gets(stringToLookIn);
printf("Second string \n");
gets(stringToFind);
if(foundWord(stringToLookIn, stringToFind, &matchingPosition))
{
printf("String found in position %d, without deleting anything \n",
matchingPosition);
}
else
{
for(int i = 0; i < strlen(stringToLookIn); i++)
{
char copyOfString[100];
memcpy(copyOfString, stringToLookIn, sizeof(copyOfString));
memmove(©OfString[i], ©OfString[i + 1], strlen(copyOfString) - i);
if(foundWord(copyOfString, stringToFind, &matchingPosition))
{
matchingPosition = ((uint32_t)matchingPosition - (uint32_t)(copyOfString));
printf("String found in position %d, after deleting position %d \n",
matchingPosition, i);
break;
}
}
}
printf("Reached end \n");
while(1);
{
}
}
答案 2 :(得分:0)
@Solomon:首先,使用gets
读取用户输入并不十分安全,因为它可能导致缓冲区溢出。请改用fgets
。其次,以下是您的问题的可能解决方案(使用fgets
):
#include<stdio.h>
#include<string.h>
#include<ctype.h>
void search(char *pat, char *txt)
{
int M = strlen(pat);
int N = strlen(txt);
char match[N]; // character array to record all possible matches
int pos[N]; // integer array to record all position: pos[0] is the match pos
int c = 0; // for indexing "match" array
if( M > N){
printf("No Exact Matching.\n");
return;
}
for(int i=0; i < M; i++){// loop throug every character in "pat"
for(int k=0; k < N; k++){ // for each character in 'txt'
if((pat[i] - txt[k]) == 0){// if a character macthes
match[c] = txt[k];
pos[c] = k;
++c;
}
}
}
match[c] = '\0';
if( (strlen(match) == strlen(pat)) && (strcmp(match, pat) == 0)){
int ndelete = strlen(txt) - strlen(pat);
int p = pos[0];
printf("The string matches at position %d, after deleting %d\n", p, ndelete);
return;
}else{
printf("No exact matching\n");
}
}
int main(void){
const int LENGTH = 100;
char txt[LENGTH], pat[LENGTH];
printf("Enter some text:\n>> ");
fgets(txt, LENGTH-1, stdin);
printf("Enter a string to find:\n>> ");
fgets(pat, LENGTH-1, stdin);
search(pat, txt);
return 0;
}
对此代码的测试如下:
gcc -std=c11 -Wall matcher.c -o a.out
./a.out
Enter some text:
>> orange
Enter a string to find:
>> age
The string matches at position 2, after deleting 3
答案 3 :(得分:-3)
在c编程中比较两个字符串的标准方法是使用strcmp()函数。如果两个字符串匹配,它将返回0,而如果两个字符串不匹配,则返回非零。
例如,
if(strcmp(pat,txt)== 0) { printf(“删除目标位置%d \ n的字符后,目标字符串匹配起始位置%d的起始位置”; i,position); }