I am trying to find the longest common prefix of two words entered by the user. I don't understand what is going wrong. I keep getting this error when I compile:

prefix.c:11:13: error: expected declaration specifiers or '...' before '(' token

And this is my code:

#include <stdio.h>
#include <string.h>
#define MAXFORTHIS 100

void prefix((char x1[]; char x2[])
{
        char x3[MAXFORTHIS];
        int a = 0;
        int b = 0;
        int c = 0;
        a = strlen(x1);
        b = strlen(x2);

        if (a < b)
                c = a;
        else
                c = b;

        for(int i = 0; i < c; ++i)
        {
                while(x1[i] == x2[i])
                        x3[i] = x1;
                if (x[i] != x2[i])
                        break;
        }

        printf("%s",x3);
}

int main(void)
{
        char x1[MAXFORTHIS];
        char x2[MAXFORTHIS];

        for(int i = 0; i < 5; i++)
        {
                printf("Enter two words: ");
                scanf(x1,x2,MAXFORTHIS,stdin);
                x1[strlen(x1)-1] = '\0';
                x2[strlen(x2)-1] = '\0';

                printf("The longest common prefix of %s and %s is ", x1,x2);
                prefix(x1,x2);
        }

        return 0;
}

Your prefix() function signature is jacked up. Fix that and you'll get other errors that are easier to fix.

What do you mean?

What do you mean?

If you don't see it after it being pointed out, I'd recommend re-reading your chapter on functions:

void prefix((char x1[]; char x2[])

Parameters are separated by a comma, not a semicolon, and you have a rogue opening paren.

I didn't see that it was a semicolon. I changed it but I still get the same error. I don't know what you mean by rogue opening paren.

void prefix((char x1[],char x2[]))

Plus I get this error, I don't know what this means:

prefix.c:41:2: error: 'for' loop initial declarations are only allowed in C99 mode
prefix.c:41:2: note: use option -std=c99 or -std=gnu99 to compile your code

I actually fixed the first error. But now I am getting:

prefix.c: In function 'prefix':
prefix.c:25:9: error: 'for' loop initial declarations are only allowed in C99 mode
prefix.c:25:9: note: use option -std=c99 or -std=gnu99 to compile your code
prefix.c:28:31: warning: assignment makes integer from pointer without a cast [enabled by default]
prefix.c: In function 'main':
prefix.c:41:2: error: 'for' loop initial declarations are only allowed in C99 mode

I don't know what you mean by rogue opening paren.

Count the parens. Two opening, one closing. Do I have to hold your hand the whole way?

Plus I get this error, I don't know what this means:

Prior to C99, you couldn't declare a variable in the initialization clause of a for loop. GCC doesn't enable C99 mode by default, yet your code uses that feature here:

for(int i = 0; i < c; ++i)

and here:

for(int i = 0; i < 5; i++)

Okay, I understand that. I took the i out and declared it outside the loop. However, now that I did that to both my for loops, it compiles, but when I run the program it is just blank. It doesn't prompt the user anymore.

int i = 0;
for(i < c; ++i;)
        {
                while(x1[i] == x2[i])
                        x3[i] = x1[i];
                if (x1[i] != x2[i])
                        break;
        }

Nevermind! I need to just declare i and put i = 0 in my for loop conditions. And it is fixed!

The program runs and compiles. When I enter the two words, it gives me a bunch of random stuff when printing.

Enter two words: economy ecology
The longest common prefix of  and § is hA(~·
commented: Grats! +17

The program runs and compiles. When I enter the two words, it gives me a bunch of random stuff when printing

That's because scanf is not used like this

scanf(x1,x2,MAXFORTHIS,stdin);

click here

Why not use scanf like this?

printf("Enter two words: ");
scanf("%s",x1);
scanf("%s",x2);

Also why use a while condition when an if else condition can be enough

for(i = 0; i < c; i++)
{
    if (x1[i] == x2[i]){
        x3[i] = x1[i];
}
else break;
}
printf("%s\n",x3);

I got the garbage letters to go away, however it is cutting off the last letter when I run the program. Do I need to make the array bigger? Or is something else going wrong?

Enter two words: program problem
The longest common prefix of progra and proble is pro
#include <stdio.h>
#include <string.h>

void prefix(char x1[], char x2[])
{
        char x3[254];
        int a = 0;
        int b = 0;
        int c = 0;
        a = strlen(x1);
        b = strlen(x2);
        int i;

        if (a < b)
                c = a;
        else
                c = b;

        for(i = 0; i < c; ++i)
        {
               if(x1[i] == x2[i])
                        x3[i] = x1[i];
                else break;
        }

        printf("%s\n",x3);
}

int main(void)
{
        char x1[254];
        char x2[254];
        int i;

        for(i = 0; i < 1; i++)
        {
                printf("Enter two words: ");
                scanf("%s %s",x1,x2);
                x1[strlen(x1)-1] = '\0';
                x2[strlen(x2)-1] = '\0';

                printf("The longest common prefix of %s and %s is ", x1,x2);
                prefix(x1,x2);
        }

        return 0;
}

Try outputting the words immediately after you input them.

Okay, I did that and they are being read properly. I think it has something to do with when I add the null character, and it is taking away that last letter and replacing it with a null one.

How do I properly add a null character?

I just need to change the - to a + and it works. Thanks everyone!

Okay, I did that and they are being read properly. I think it has something to do with when I add the null character, and it is taking away that last letter and replacing it with a null one.

How do I properly add a null character?

What makes you think you have to add it? If you do a strlen() and it works, doesn't that mean there already is a null?

I went back and figured that out and saw that I needed to make it a + and not a - when I was using strlen(x1)+1

I went back and figured that out and saw that I needed to make it a + and not a - when I was using strlen(x1)+1

I repeat -- WHY?
Why do you now need to add a null after the null that's already there? That's what your + is doing.

Directly to the point: Why do you need to add the null at all?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.