tags:

views:

103

answers:

4

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;           
}
+6  A: 

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

See: strncpy and strncmp

PeterK
+3  A: 

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

Nathan Fellman
yes :):) that was a stupid mistake :)
fahad
+1  A: 

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

Mau
A: 

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

Helen Neely