Reputation: 80
This for Homework
So for my project I have to combine two strings in a from where both the strings have a pattern when combined. (That's pretty vague so ill put an example down below. My issue is with my argv argument in my main function. Argv reads the input of the user when the program is being ran. So it would be like ./program_name -r. The -r for this part of the program would make it so the example shown below would be ran through after the user input. However the problem I'm having is if I have any other letter like -d then the program still runs through. This wouldn't be an issue but another part of my program requires me to have a different run code so the program would do something different. I assume my issue is within my if statement but I can't figure out why that isn't working. Any help would be appreciated!
Input: String 1: abc
String 2: 123
Output:a1b2c3
Here's my program, it complies and gives the correct output
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void merge(char *s1, char *s2, char *output)
{
while (*s1 != '\0' && *s2 != '\0')
{
*output++ = *s1++;
*output++ = *s2++;
}
while (*s1 != '\0')
*output++ = *s1++;
while (*s2 != '\0')
*output++ = *s2++;
*output='\0';
}
int main(int argc , char *argv[])
{
int i;
if(argv[i] = "-r") {
char string1[30];
char string2[30];
printf("Please enter a string of maximum 30 characters: ");
scanf("%s" ,string1);
printf("Please enter a string of maximum 30 characters: ");
scanf("%s" ,string2);
char *output=malloc(strlen(string1)+strlen(string2)+1);
//allocate memory for both strings+1 for null
merge(string1,string2,output);
printf("%s\n",output); }
return 0; }
Upvotes: 2
Views: 259
Reputation: 5525
What you made wrong has been answered by C8263A20 already but comparing strings is not how it is done.
Note: parsing command-line options is a rather complex task and you should use ready made solutions like e.g.: getopt(3)
(in Posix) if possible!
A short and (heavily over-)simplified solution to your current problem would be:
#include <stdio.h>
int main(int argc, char *argv[])
{
// check: do we have one option given at all?
if (argc == 2) {
// first character is a hyphen, so skip it
// A good idea would be to check if the assumption above is correct
switch (argv[1][1]) {
// we can only "switch" integers, so use single quotes
case 'r':
puts("Option \"r\" given");
break;
case 'd':
puts("Option \"d\" given");
break;
default:
printf("Unknown option %c given\n", argv[1][1]);
break;
}
} else {
puts("No options given at all");
}
return 0;
}
If you do it this way (with a switch
) you can easily add more single letter options without cluttering your code. Put it in a loop and you can give the program more options at once.
Upvotes: 2
Reputation: 26
What you're trying to do in your main function is to check whether the first command-line argument equals "-r".
What you are actually doing is assigning "-r" to argv[i] by using a single '='. In C, comparisons are done via the '==' Operator.
However, since you are trying to compare to Strings (which are char-Arrays in C) you must not use '==' for this purpose, since you would only compare the Addresses at which the char-Arrays begin.
Instead use the C library function strcmp for this purpose:
https://www.tutorialspoint.com/c_standard_library/c_function_strcmp.htm
You also haven't initialized the variable i as Ora already pointed out.
Upvotes: 1
Reputation: 66
Upvotes: 1