c - 如果条件不起作用

标签 c if-statement

我的目标是取两个字符串并比较它们的结尾,如果它们都以“ing”,“ed”结尾,或者结尾不匹配。它总是说字符串不匹配。

#include <stdio.h>
#include <conio.h>
#include <string.h>
int ised(char str[]);
int ising(char str[]);

int  main()
{
    char str1[30],str2[30];
    printf("Enter 1st string:\n");
    gets(str1);
    printf("Enter 2nd string:\n");
    gets(str2);
    if((ising(str1))||(ised(str1))||(ising(str2))||(ised(str2)))
    {
        if(ising(str1)&&ising(str2))
        {
            printf("Both strings end with ing");
        }
        else if(ised(str1)&&ised(str2))
        {
        printf("Both strings end with ed");
        }
        else
        printf("Both strings ending do not match");
    }
    else
    printf("One or both strings do not end with ing or ed.Program Quitting...");
    getch();
    return 0;
}
int ising(char str[])
{
    int len,flag=0;
    len=strlen(str);

        if (!(strncpy(&str[len-3],"ing",3)))
                flag=1;
        else
        flag=0; 
return flag;           
}
int ised(char str[])
{
    int len,flag=0;
    len=strlen(str);

        if( !(strncpy(&str[len-2],"ed",2)) )
                flag=1;
        else
        flag=0; 
return flag;           
}

最佳答案

您正在使用 strncpy 进行字符串复制,如果您想比较字符串,请使用 strncmp

参见:strncpystrncmp

关于c - 如果条件不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3254706/

相关文章:

c - 错误 : 'strdup' was not declared in this scope

c - 1's and 0' 到十六进制的数组

sql - 为 Postgres 函数设置默认返回值

if-statement - 根据数组值在 SwiftUI 中有条件地文本

if-statement - 圈复杂度 = 1 + #if 语句?

javascript - React - 从子组件更新状态,然后根据状态执行某些操作

c++ - malloc/free 用于调整数组大小

c - 为什么要求最大整数的代码会给出随机输出?

c - 如何初始化包含 union 数组的结构

JQuery 从多选列表 if 语句获取值 MVC3