0

I'm testing a small program which basically compares whether 2 input strings are identical (as strcmp does).

I'd want to do something like (users type 2 desired strings on the same line). In this case it should return "The two strings are different"

./a.out foo bar 

should I do this to read the user's input strings?

 scanf("%s %s", str1, str2);

or

gets(str1); gets(str2);

Here is what I have so far (it seems to stuck in an infinite loop for some reasons)

int mystrcmp(char str1[], char str2[]) {
    int i = 0;
    while (str1[i] == str2[i]) {
        if (str1[i] == '\0' || str2[i] == '\0') break;
        i++;
    }
    if (str1[i] == '\0' && str2[i] == '\0')
        return 0;
    else
        return -1;
}

int main(int argc, char * * argv) {
    int cmp;
    char str1[1000], str2[1000];
    scanf("%s %s", str1, str2);
    //gets(str1); gets(str2);
    cmp = mystrcmp(str1, str2);
    if (cmp == 0)
        printf("The two strings are identical.\n");
    else
        printf("The two strings are different.\n");
    return 0;
}
2
  • 2
    The strings passed in the command line are passed in argv. Commented Sep 4, 2015 at 18:18
  • 1
    For the record, you should usually use fgets instead of gets to prevent buffer overflow. What if I wrote two 1001 character words that differed on the last letter? Your program wouldn't be able to handle that. fgets lets you set the number of characters to read to be safe (which should be 1 less than the total size of your buffer). Commented Sep 4, 2015 at 18:21

2 Answers 2

4

You should do neither. Instead I suggest you learn about how command line arguments are passed to the main function through the argc and argv arguments.

I suggest you try this program to help your understanding:

#include <stdio.h>

int main(int argc, char *argv[])
{
    printf("argc = %d\n", argc);

    for (int a = 0; a < argc; ++a)
        printf("argv[%d] = \"%s\"\n", a, argv[a]);
}

For your example invocation

./a.out foo bar

the program above will print

argc = 3
argv[0] = "./a.out"
argv[1] = "foo"
argv[2] = "bar"
Sign up to request clarification or add additional context in comments.

4 Comments

I see. So do I pass the first input string thourgh argv[1] and second through argv[2] ?
Yes. And always check argc for the correct number of parameters. Otherwise you will likely get some segfaults.
argv is an array of char *s. Each element points to a string.
@Walle Yes those two entries in the arrays contain pointers to the first two arguments. However, always check that you have the correct number or arguments (in your case argc should be at least 3).
3

This solution should work:

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

int main(int argc, char *argv[]) {
    if(argc != 3) {
        puts("Wrong number of arguments");
        return 0;
    }

    if(strcmp(argv[1], argv[2]))
        puts("The two strings are different.");
    else
        puts("The two strings are identical.");
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.