开发者

if condition not working

My aim is to take two strings and compare there ends if both of them ends with "ing","ed" or there ends do not match.It always says that strings do not match .

#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;           
}


You are using strncpy which does string copying, if you want to compare strings, use strncmp.

See: strncpy and strncmp


It looks like you're using strncpy when you actually mean to use to strcmp. You're trying to compare the strings, right?


Use strncmp. And if you're slicing strings, don't forget the terminators.


Would be nice to tell us where you're having problems, what you expect and the result you're currently getting.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜