【发布时间】:2017-11-13 07:55:26
【问题描述】:
我必须编写一个名为 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。 如果有人可以帮助我,将不胜感激。
【问题讨论】:
-
st1[], st2[]所需的元素/类型的数量取决于lenst1, lenst2和先前的代码if (lenst2>lenst1)不好,因为lenst1, lenst2尚未分配/初始化。if()想做什么? -
这些字符串显示为未定义,因为您从未真正定义过变量。阅读 C 中字符串的定义。您要查找的类型是
char * -
请注意,您不能只定义任意函数并运行它。 C 程序至少需要一个
main函数。看来你需要回去做一个基本的 C 教程或阅读基本的 C 书,然后再继续。
标签: c arrays string parameters arguments