在更大的字符串中查找子字符串的位置 [英] Finding the position of a substring in a larger string
问题描述
我已经创建了一个函数,它应该在一个较大的字符串中找到子字符串的第一个字符的数字位置。我有一些输出的问题,我不太清楚为什么。这些问题包括每次返回-1而不是子字符串的整数位置。我已经调试,并不能跟踪功能出错的地方。
函数应该如何执行:如果我的字符串是狗很快,而我正在搜索子字符串dog,函数应该返回4。
以下是函数:
int findSubString(char original [],char toFind []){
size_t i,j;
int originalLength = 0;
int toFindLength = 0;
originalLength = strlen(original)+ 1;
toFindLength = strlen(toFind)+ 1; (i = 0; i for(j = 0; j if( toFind [j] =='\0'){
return i;
if(original [i + j]!= toFind [j]){
break;
if(original [i] =='\0'){
return -1;
函数参数不能修改,这是一个要求。任何帮助表示感谢!
解决方案这些语句在循环内部
<$ p $ if(toFind [j] =='\0'){
return i;
$ b $ p
$ b
导致未定义的行为,因为字符串 toFind 可以比字符串原始
。
同样适用于这个循环如果(原始的[i + j]!= toFind [j]){
break; p>
$ / code>
因为 i + j
可以大于字符串的长度原来的
。
并且没有必要扫描所有字符字符串原来的
,如果你打算在里面找到一个子字符串的话。
字符串原始
不小于字符串 toFind
。
$ b的长度$ b 如果你想在字符串 original
中只找到字符串 toFind
的第一个字符,足够使用标准C函数 strchr
。如果要在字符串 original
中查找整个字符串 toFind
,则可以使用另一个C标准函数<$ c
如果你想自己编写函数来查找其他字符串中的字符串,那么它可以看下面的例子方式
我声明的功能像
long long int findSubString const char original [],const char toFind []);
然而,您可以按照自己的喜好编写自己的声明,例如
int findSubString(char original [],char toFind []);
但是在这种情况下,您应该声明函数局部变量 success
like
int success = -1;
并使用格式说明符%d$ c
$ b $
#include< stdio.h>
#include< string.h>
#include< stddef.h>
long long int findSubString(const char original [],const char toFind [])
{
size_t n = strlen(original);
size_t m = strlen(toFind);
long long int success = -1; (!(n {
n = n - m + 1;
;
for(size_t i = 0; success == -1&< n; i ++)
{
size_t j = 0; (j< m&& original [i + j] == toFind [j])j ++;
if(j == m)success = i;
}
}
返回成功;
$ b $ int main(void)
{
printf(%lld\\\
,findSubString(The dog was fast,dog) );
返回0;它的输出是
$ b 4
I have created a function that should find the numerical position of the first character of a substring in a larger string. I am having some problems with the output and I am not too sure why. These problems include -1 being returned every single time instead of the integer position of the substring. I have debugged and cannot trace where the function goes wrong.
This is how the function should perform: If my string is "The dog was fast" and I am searching for the substring "dog", the function should return 4. Thanks to chqrlie for help with the loop.
Here is the function:
int findSubString(char original[], char toFind[]) {
size_t i, j;
int originalLength = 0;
int toFindLength = 0;
originalLength = strlen(original) + 1;
toFindLength = strlen(toFind) + 1;
for (i = 0; i < toFindLength + 1; i++) {
for (j = 0; j < originalLength + 1; j++) {
if (toFind[j] == '\0') {
return i;
}
if (original[i + j] != toFind[j]) {
break;
}
}
if (original[i] == '\0') {
return -1;
}
}
}
The function parameters cannot be modified, this is a requirement. Any help appreciated!
解决方案 These statements inside the loops
if (toFind[j] == '\0') {
return i;
}
results in undefined behavior because the string toFind
can be shorter than the string original
.
The same is valid for this loop
if (original[i + j] != toFind[j]) {
break;
}
because i + j
can be greater than the length of the string original
.
And there is no need to scan all characters of the string original
if you are going to find a substring inside it.
Also you should check whether the length of the string original
is not less than the length of the string toFind
.
If you want to find only the first character of the string toFind
in the string original
it is enough to use standard C function strchr
. If you want to find the whole string toFind
in the string original
then you could use another C standard function strstr
.
If you want to write the function yourself to find a string in other string then it can look for example the following way
I declared the function like
long long int findSubString( const char original[], const char toFind[] );
however you can write its declaration as you like for example like
int findSubString( char original[], char toFind[] );
But in this case you should declare function local variable success
like
int success = -1;
and output the result using format specifier "%d"
instead of "%lld"
.
Here you are.
#include <stdio.h>
#include <string.h>
#include <stddef.h>
long long int findSubString( const char original[], const char toFind[] )
{
size_t n = strlen( original );
size_t m = strlen( toFind );
long long int success = -1;
if ( !( n < m ) )
{
n = n - m + 1;
for ( size_t i = 0; success == -1 && i < n; i++ )
{
size_t j = 0;
while ( j < m && original[i+j] == toFind[j] ) j++;
if ( j == m ) success = i;
}
}
return success;
}
int main(void)
{
printf( "%lld\n", findSubString( "The dog was fast", "dog" ) );
return 0;
}
Its output is
4
这篇关于在更大的字符串中查找子字符串的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!