我姐姐和我最近在C中得到了一个编写一个函数,用于在字符串中查找特定单词,如果找到,则返回字符串中找到的该单词的第一个字母索引。但是出现了各种各样的问题......
#include <stdio.h>
#include "u_s_search_word_in_string.h"
int main()
{
int first_letter_idx = 0;
char line[32] = "Janen is a dog";
char word[4] = "dog";
first_letter_idx = search_word_in_string( line, word );
printf("\nThe beginning of the word is at index %d.\n", first_letter_idx );
return 0;
};
#include <stdio.h>
#include "u_s_search_word_in_string.h"
int search_word_in_string ( char line[], /* ( Input ) */
char word[] /* ( Input ) Word to search for in {line}. )
{
int line_idx = 1;
int word_idx = 1;
int first_letter_idx = 0;
/* Finding word. */
while ( (line[line_idx] != '\0') )
{
if ( (line[line_idx] = word[word_idx]) && (word[word_idx] != '\0') )
{
/* Index of beginning letter of word set to another variable. */
if ( (first_letter_idx = 0) )
{
first_letter_idx = line_idx;
}
line_idx++;
word_idx++;
}
else
{
/* If word turns out to be not found, continue. */
first_letter_idx = 0;
line_idx++;
word_idx++;
};
};
if ( (first_letter_idx = 0) )
{
puts("Cannot find word in string.");
return -1;
};
if ( first_letter_idx >= 1 )
{
puts("Found word, returning first letter index.");
};
return (first_letter_idx);
};
int search_word_in_string ( char line[], /* ( Input ) */
char word[] /* ( Input ) Word to search for in {line}. */ );
很抱歉,如果评论看起来很糟糕,我会在以后再处理,但我不知道有什么问题......
首先,在编译时没有关于使用Puts()的警告,第二,编译告诉我一切都成功但是当我执行它时,会发生这种情况:
./geany_run_script.sh: 5: ./geany_run_script.sh: ./testing_word_search: not found
我的输入构建命令:
编译:gcc -Wall -c testing_word_search.c u_s_search_word_in_string.c Build:gcc -o word_search testing_word_search.o u_s_search_word_in_string.o执行:“./% e”
通常的默认设置,我知道如果我想运行一个程序,我必须去主模块。
当我使用终端运行它时,它说:
The beginning of the word is at index 0.
除了技术上还应该有另一条消息。
如果你们中的一位成员指出我的错误,我真的很感激,因为到目前为止,我对我可能做错了什么一无所知。
答案 0 :(得分:2)
您的代码存在一些问题:
}
之后),您不需要分号。==
而不是=
来比较您的字符(首先是比较,第二项是作业)。line_idx
和word_idx
应为零(因为您要查找的字符串也可以在开头存在)。first_letter_idx
应该默认为-1(因为,0是一个有效值),你应该检查-1而不是0来知道搜索是否失败。word[word_idx] != '\0'
是不必要的;同样line_idx++;
和word_idx++;
可以写在if-else语句下面,因为无论发生什么事情都会被处决。)我希望这能解决你的大部分问题。
答案 1 :(得分:0)
标题string.h
提供了一个函数strstr
,它是为您要完成的工作量身定制的。完整的例子:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
char *string = NULL;
char *substr = NULL;
char *p = NULL;
printf ("\nEnter string : ");
scanf ("%m[^\n]%*c", &string);
printf ("\nEnter substr : ");
scanf ("%m[^\n]%*c", &substr);
if ((p = strstr (string, substr)) != NULL)
printf ("\nSubstring found beginning at : %c (char '%lu' in string)\n\n",
*p, p - string + 1);
else
printf ("\nSubstring NOT in string.\n\n");
if (string) free (string);
if (substr) free (substr);
return 0;
}
<强>输出:强>
$ ./bin/strstrc
Enter string : Janen is a dog
Enter substr : dog
Substring found beginning at : d (char '12' in string)
如果您必须在不使用strstr
的情况下执行此操作,则评论中的链接将有所帮助。
答案 2 :(得分:0)
检查以下代码:
#include <stdio.h>
#include<string.h>
int search_word_in_string ( char line[], /* ( Input ) */
char word[] )
{
int line_idx = 0;
int k;
int word_idx = 0;
int first_letter_idx = 0;
/* Finding word. */
while ( (line[line_idx] != '\0') )
{
word_idx =0;
if(line[line_idx] == word[word_idx])
{
k = line_idx;
while(word[word_idx] != '\0' && word[word_idx] == line[k])
{
k++;
word_idx++;
}
if((k-line_idx) == strlen(word))
return line_idx+1;
}
line_idx++;
}
return -1;
}
int main()
{
int first_letter_idx = 0;
char line[32] = "Janen is a dog";
char word[4] = "is";
first_letter_idx = search_word_in_string( line, word );
if(first_letter_idx > -1)
printf("\nThe beginning of the word is at index %d.\n", first_letter_idx );
else
printf("\n String not found \n");
return 0;
}
答案 3 :(得分:0)
非常感谢Philipp和Bluepixy,我更新了我的代码:
/*
* Author: Usia, Sebastian
* Date: 21/NOV/2014
*
* Input: Two character type variables.
* Output: Index number of beginning letter of word.
*
* Description: Uses an algorithm to find a word in a string and if found
* it returns the index number of the first letter of the found word.
*
*/
#include <stdio.h>
#include "u_s_search_word_in_string.h"
int search_word_in_string ( char line[], /* ( Input ) */
char word[] /* ( Input ) Word to search for in {line}. */ )
{
int line_idx = 0;
int word_idx = 0;
int first_letter_idx = 0;
int word_length = 0;
int matched = 0; /* Number of consecutive characters matching between {word} and {line}. */
/* Find {word}'s length. */
while (word [word_length] != '\0') word_length++;
printf("word length is %d\n", word_length);
/* Finding word. */
while ( (line[line_idx] != '\0') && (matched != word_length) )
{
printf("before IF: line_idx = %d, word_idx = %d\n", line_idx, word_idx);
if ( (word[word_idx] != '\0') && (line[line_idx] == word[word_idx]) )
{
printf("*match* line_idx = %d, word_idx = %d\n", line_idx, word_idx);
/* Marking {word}'s first letter in the text line. */
if ( (word_idx == 0) )
{
first_letter_idx = line_idx;
}
matched++;
word_idx++;
}
else
{
printf("letters do not match --- line_idx = %d, word_idx = %d\n",line_idx, word_idx);
/* If letters don't match, reset word_idx and match counter. */
word_idx = 0;
matched = 0;
};
line_idx++; /* No matter what we move ahead the {line_idx}. */
};
printf("matched %d\n", matched );
if ( matched == word_length )
{
printf("Found word, returning first letter index.");
return (first_letter_idx);
}
else
{
printf("Cannot find word in string.");
return -1;
};
};
添加痕迹只是为了确定。
我从来没有真正听说过&#34; strstr&#34;之前,我会很快检查出来的。再次感谢。