c - 如何正确编写参数函数?

标签 c arrays string parameters arguments

我必须使用名为 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/

相关文章:

c - 用宏在 C 中初始化 LUT?

c - 当我在笔记本电脑上运行时,我的代码可以工作,但是当我将其上传到 hackerrank 时,我在 strtol 处收到错误

java - 如何在二维中使用字符串数组(String[][])并解析每个空间(指定元素点)中的数据?

不同文件中枚举和数组的一致性检查

javascript,搜索我的名字

Javascript 字符串比较未显示正确结果

c - scanf() 将换行符保留在缓冲区中

c - printf 只打印 "1537"而不是整数的值

arrays - 根据 Choices 创建一个执行特定输出的数组

java - 在java中使用多个分割参数分割字符串