我必须使用名为 contains.c 的 C 程序,它接受两个文本字符串作为参数,如果第二个字符串完全包含在第一个字符串中,则打印“true”后跟换行符,否则打印“false”后跟换行符。
例如(在命令提示符下)
$ ./contains 'I have a really bad feeling about this' 'bad feeling'
true
$ ./contains 'To be or not to be' 'That is the question'
false
$ ./contains 'I am the walrus' 'I am the walrus'
true
$ ./contains 'the walrus' 'I am the walrus'
false
$ ./contains 'kmjnhbvc45&^$bn' '.'
false
到目前为止,这是我的代码。
#include <stdio.h>
#include <string.h>
int main( int argc, char* argv[])
{
int i;
int j;
int lenst1;
int lenst2;
int pos1;
int pos2;
if (lenst2>lenst1)
{
printf("false");
return 0;
}
for (j=0; j<lenst1;j++)
{
for (i=0; i<lenst2; i++)
{
if (st2[i]==st1[j]) //NOT SURE HOW TO DEFINE "st2" and "st1"
{
pos1=j;
pos2=i;
while (pos2<lenst2)
{
pos2++;
pos1++;
if (st2[i]==st1[j])
{
}
else
{
printf("false\n");
return 0;
}
printf("true\n");
return 0;
}
}
}
}
}
我不完全确定我的代码中有多少缺陷,但 Xcode 声称需要定义 st2 和 st1。 如果有人能帮助我,我将不胜感激。
最佳答案
您忘记阅读您的论点。
char* st1 = argv[1];
char* st2 = argv[2];
lenst1 = strlen(st1);
lenst2 = strlen(st2);
关于c - 如何正确编写参数函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44489278/