[英]How to find string position within another string in C recursively?
我們有一個分配來創建帶有兩個字符串參數的遞歸函數, 原型應如下所示 :
int instring(char* word, char* sentence);
如果我們調用該函數 :
instring("Word", "Another Word");
它應該具有以下返回值:
我可以使用保存位置的第三個參數來執行此操作,但是很遺憾,我們不允許使用兩個以上的參數。
所以問題是我如何使其工作? 到目前為止,這是我想出的:
int instring(char* word, char* sentence) {
if (*word == '\0') {
return 0;
} else if (*word != '\0' && *sentence == '\0') {
return -1;
} else {
if (*word == *sentence) {
instring(word+1, sentence+1);
} else {
instring(word, sentence+1);
}
}
}
如果可以找到“ word”,則得到0,否則得到-1。 由於我無法在函數調用之間存儲任何值,因此無法獲得'word'字符串開始的位置。 除了外部變量和只有兩個輸入字符串,還有其他方法可以獲取位置嗎?
int instring( char* word, char* sentence ){
int lenw = strlen(word);
int lens = strlen(sentence);
if(lenw > lens) return -1;
if(strncmp(sentence, word, lenw)==0)
return 0;
else {
int ret = instring(word, sentence + 1);
if(ret < 0)
return ret;
return 1 + ret;
}
}
我不認為這些是特別優雅的解決方案,但是問題也不是很優雅(標准解決方案是迭代的,遞歸也不是最佳的)。 這是效率很低的純遞歸解決方案(無迭代):
#include <stdio.h>
static int const debug = 0;
static int instring(char const *word, char const *sentence)
{
int rc;
if (debug)
printf("-->> [%s] in [%s]\n", word, sentence);
if (*word == '\0')
rc = 0;
else if (*sentence == '\0')
rc = -1;
else if (*word != *sentence || (rc = instring(word+1, sentence+1)) != 0)
{
if ((rc = instring(word, sentence+1)) >= 0)
rc++;
}
if (debug)
printf("<<-- [%s] in [%s] = %d\n", word, sentence, rc);
return rc;
}
int main(void)
{
char word[] = "Word";
char str1[] = "Another Word";
char str2[] = "Wrong parts of the data";
char str3[] = "A Word Or Two";
printf("Search for [%s] in [%s] at offset %d\n",
word, str1, instring(word, str1));
printf("Search for [%s] in [%s] at offset %d\n",
word, str2, instring(word, str2));
printf("Search for [%s] in [%s] at offset %d\n",
word, str3, instring(word, str3));
return 0;
}
如果設置debug = 1;
您會明白為什么它效率低下。 它使用變量rc
簡化調試跟蹤。
這是一種更有效的選擇,因為當第一個字符匹配時,它使用迭代來限制搜索。 不難看出如何刪除遞歸剩下的內容(這是簡單的尾遞歸),從而留下一個完全迭代的解決方案,這是解決此問題的正常方法。
#include <stdio.h>
static int instring(char const *word, char const *sentence)
{
int rc;
if (*word == '\0')
return 0;
if (*sentence == '\0')
return -1;
if (*word == *sentence)
{
int i;
for (i = 1; word[i] != '\0' && word[i] == sentence[i]; i++)
;
if (word[i] == '\0')
return 0;
}
if ((rc = instring(word, sentence+1)) >= 0)
rc++;
return rc;
}
int main(void)
{
char word[] = "Word";
char str1[] = "Another Word";
char str2[] = "Wrong parts of the data";
char str3[] = "A Word Or Two";
printf("Search for [%s] in [%s] at offset %d\n",
word, str1, instring(word, str1));
printf("Search for [%s] in [%s] at offset %d\n",
word, str2, instring(word, str2));
printf("Search for [%s] in [%s] at offset %d\n",
word, str3, instring(word, str3));
return 0;
}
樣本輸出:
Search for [Word] in [Another Word] at offset 8
Search for [Word] in [Wrong parts of the data] at offset -1
Search for [Word] in [A Word Or Two] at offset 2
兩種版本中的測試代碼都可以得到改進(通過檢查結果是否符合預期,並使用測試功能而不是三遍寫這么多代碼,還可以使用循環掃描測試數據)。
int len1 = strlen(word);
int len2 = strlen(sentence);
int n,k=0;
for(int i =0;i<len2;i++){
n =i;
while(sentence[n] == word[k] && n<len2){
if(k==len1 && ( n==len2 || sentence[n+1] ==' ')
return i;//if word found return the position in the original string//
n++;
k++;
}
k =0;
while(sentence[i] != ' ' && i<len2) //go to next word//
i++;
}
return -1;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.