I really want to write this program in c without c++ coding. Please help me convert my code to c only. Here is sample input from a text file:
alex
busted
marries
marries
test
teste
tested
smile
smiled
works
worked
indexed
hello
kkked
holddd
smexexed
whitespaced
and Here is output using ed as my suffix
busted
tested
smiled
worked
indexed
kkked
smexexed
whitespaced

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>

void create(FILE *in, FILE *out, const char *suffix);
int substr_at_the_end(const char [], const char []);

int main()
{
        printf("Enter suffix: ");
        char suffix[50];
        scanf("%s", &suffix);

        FILE *input = fopen("words.txt", "r");
        FILE *out = fopen("wordsout.txt", "w");

        create(input, out, suffix);

        printf("Success!\n");
        fclose(input);
        fclose(out);

        getch();
        return 0;
}

void create(FILE *in, FILE *out, const char *suffix)
{
        int size = 0;
        char line[50];
        char *words[100];

        if (in == NULL || out == NULL)
        {
                printf("File opening/writing failed!\n");
                getch();
                exit(1);
        }

        fscanf(in, "%s", line);
        words[size] = (char *)malloc(sizeof(char) * 50);
        strcpy(words[size], line);
        size++;

        while (fgets(line, 50, in))
        {
                words[size] = (char *)malloc(sizeof(char) * 50);
                fscanf(in, "%s", line);
                strcpy(words[size], line);
                size++;
        }

        int flag = 0;
        for (int i = 0; i < size; i++)
                if (substr_at_the_end(words[i], suffix))
                {
                        flag = 1;
                        fprintf(out, "%s\n", words[i]);
                }

        if (flag == 0)
                fprintf(out, "Couldn't find any words with suffix %s!\n", suffix);

        for (int i = 0; i < size; i++)
                free(words[i]);
}

int substr_at_the_end(const char str[], const char substr[])
{
        if (strlen(substr) > strlen(str))
                return 0;

        if ((strlen(substr) == 1) && (strcmp(substr, &str[strlen(str) - 1]) != 0))
                return 0;

        char tmp = substr[0];
        int pos = -1;
        for (unsigned int i = 0; i < strlen(str); i++)
                if (tmp == str[i])
                        pos = i;
                
        if (pos == -1)
                return 0;

        int flag = 0;
        char holder[100];
        int holder_size = 0;
        for (unsigned int i = pos; i <= strlen(substr) + pos - 1; i++)
        {
                strcpy(&holder[holder_size], &str[i]);
                holder_size++;
        }

        holder[strlen(substr)] = '\0';
        if (!strcmp(holder, substr) && (holder[strlen(substr) - 1] == str[strlen(str) - 1]))
                flag = 1;

        return flag;
}

You open the files in main() , you don't check if they opened properly until you've called your function. Bad form. Open and check immediately. That way you don't waste time and confuse your brain cells later.

Then you use scanf("%s"...) which is dangerous. See this, then jump to Part 7.

This article has been dead for over six months. Start a new discussion instead.