此的输出作业参数通过argv的影响,通过该程序
所以对我的项目,我有两个字符串在一个从那里合并当两个字符串有一个模式相结合。 (这非常模糊,所以生病把下面的例子放在下面,我的问题是在我的main函数中使用了argv参数,当程序正在运行时,argv读取用户的输入,所以它就像./program_name -r。 -r对于程序的这一部分来说是这样的,所以下面显示的例子会在用户输入后被运行,但是我遇到的问题是如果我有任何其他类似-d的字母,程序仍然会运行。不会是一个问题,但我的程序的另一部分需要我有一个不同的运行代码,所以程序会做一些不同的事情。我认为我的问题是在我的if语句,但我不明白为什么这是行不通的。任何帮助,将不胜感激
输入:字符串1:ABC
字符串2:123
输出:A1B2C3
这是我的节目,它符合并给出了正确的输出
#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; }
我正在考虑为这个程序使用switch语句,但我不知道它是如何完成。这帮助了一大堆。谢谢! – chrisHG