根据命令行参数将字符串合并在一起。
Merging strings together based on command line arguments.
我正在尝试用 C 编写一个程序,根据给定的命令行参数以两种不同的方式将 2 个字符串合并在一起。
'-i'交替合并字符串,像这样
combine_strings –i
请输入最多30个字符的字符串:abcde
请输入最多30个字符的字符串:1234567
合并后的字符串是:a1b2c3d4e567
'-w' 将两个字符串连接在一起,并在字符串的每个字符之间插入一个'*',像这样
combine_strings-w
请输入最多30个字符的字符串:abcde
请输入最多30个字符的字符串:1234567
合并后的字符串为:abcde*1*2*3*4*5*6*7
这是我目前得到的。
#include<stdio.h>
#include<string.h>
#include<memory.h>
void alt_merge(char *string1,char *string2,char *stringfinal){
while (*string1 != '[=10=]' && *string2 != '[=10=]')
{
*stringfinal++= *string1++;
*stringfinal++ = *string2++;
}
while (*string1 != '[=10=]')
*stringfinal++=*string1++;
while (*string2 != '[=10=]')
*stringfinal++ = *string2++;
*stringfinal='[=10=]';
}
void concate_star(char *string1,char *string2,char *stringfinal){
strcpy(stringfinal,strcat(string1,string2));
}
int main(int argc,char *argv[]){
char *string1[30+1];
char *string2[30+1];
char *stringfinal=malloc(strlen(string1)+strlen(string2)+1);
if(argv[1]=='-i'){
printf("Please enter a string of maximum 30 characters: ");
scanf("%s",&string1);
printf("Please enter a string of maximum 30 characters: ");
scanf("%s",&string2);
alt_merge(string1,string2,stringfinal);
printf("The combined string is: %s",stringfinal);
}
else if(argv[1]=='-w'){
printf("Please enter a string of maximum 30 characters: ");
scanf("%s",&string1);
printf("Please enter a string of maximum 30 characters: ");
scanf("%s",&string2);
concate_star(*string1,*string2,*stringfinal);
printf("The combined string is: %s",stringfinal);
}
return 0;
}
我收到这些错误消息。
jdoodle.c: In function 'main':
jdoodle.c:27:23: warning: implicit declaration of function 'malloc' [-Wimplicit-function-declaration]
char *stringfinal=malloc(strlen(string1)+strlen(string2)+1);
^~~~~~
jdoodle.c:27:23: warning: incompatible implicit declaration of built-in function 'malloc'
jdoodle.c:27:23: note: include '<stdlib.h>' or provide a declaration of 'malloc'
jdoodle.c:27:37: warning: passing argument 1 of 'strlen' from incompatible pointer type [-Wincompatible-pointer-types]
char *stringfinal=malloc(strlen(string1)+strlen(string2)+1);
^~~~~~~
In file included from jdoodle.c:2:0:
/usr/include/string.h:384:15: note: expected 'const char *' but argument is of type 'char **'
extern size_t strlen (const char *__s)
^~~~~~
jdoodle.c:27:53: warning: passing argument 1 of 'strlen' from incompatible pointer type [-Wincompatible-pointer-types]
char *stringfinal=malloc(strlen(string1)+strlen(string2)+1);
^~~~~~~
In file included from jdoodle.c:2:0:
/usr/include/string.h:384:15: note: expected 'const char *' but argument is of type 'char **'
extern size_t strlen (const char *__s)
^~~~~~
jdoodle.c:29:17: warning: multi-character character constant [-Wmultichar]
if(argv[1]=='-i'){
^~~~
jdoodle.c:29:15: warning: comparison between pointer and integer
if(argv[1]=='-i'){
^~
jdoodle.c:35:23: warning: passing argument 1 of 'alt_merge' from incompatible pointer type [-Wincompatible-pointer-types]
alt_merge(string1,string2,stringfinal);
^~~~~~~
jdoodle.c:5:6: note: expected 'char *' but argument is of type 'char **'
void alt_merge(char *string1,char *string2,char *stringfinal){
^~~~~~~~~
jdoodle.c:35:31: warning: passing argument 2 of 'alt_merge' from incompatible pointer type [-Wincompatible-pointer-types]
alt_merge(string1,string2,stringfinal);
^~~~~~~
jdoodle.c:5:6: note: expected 'char *' but argument is of type 'char **'
void alt_merge(char *string1,char *string2,char *stringfinal){
^~~~~~~~~
jdoodle.c:39:22: warning: multi-character character constant [-Wmultichar]
else if(argv[1]=='-w'){
^~~~
jdoodle.c:39:20: warning: comparison between pointer and integer
else if(argv[1]=='-w'){
^~
jdoodle.c:45:44: warning: passing argument 3 of 'concate_star' makes pointer from integer without a cast [-Wint-conversion]
concate_star(*string1,*string2,*stringfinal);
^
jdoodle.c:19:6: note: expected 'char *' but argument is of type 'char'
void concate_star(char *string1,char *string2,char *stringfinal){
^~~~~~~~~~~~
我是编程新手,非常感谢任何帮助和建议。
两个问题,你没有在函数malloc()
的声明处包含stdlib.h
。
此外,您向 strlen()
传递了一个错误的参数,但您传递了 char**
。其他功能也一样。您的参数不匹配。
char *string1[30+1];
char *string2[30+1];
你想要
char string1[30+1];
char string2[30+1];
在比较 "-i"
和 argv[1]
时,您还应该使用 strcmp
。
现在 argv
基本上是 char*
的数组。您基本上是在比较空终止字符数组和 (string) 与 "-i"
。 (不是 '-i'
)。
还注意到一件有趣的事情,
编译器说
jdoodle.c:29:15: warning: comparison between pointer and integer
if(argv[1]=='-i'){
^~
An ordinary character literal that contains more than one c-char is a
multicharacter literal . A multicharacter literal has type int and
implementation-defined value.
'-i'
是多字符文字。这有类型 int
这就是 pointer
和 int
.
之间的警告比较的原因
还有一件事你应该清楚地知道,当你将数组传递给函数时,它会衰减为指向数组第一个元素的指针。
char *s[100]
衰减为 char**
因为数组的内容是 char*
指向它的指针将是 char**
.
我正在尝试用 C 编写一个程序,根据给定的命令行参数以两种不同的方式将 2 个字符串合并在一起。
'-i'交替合并字符串,像这样
combine_strings –i 请输入最多30个字符的字符串:abcde 请输入最多30个字符的字符串:1234567 合并后的字符串是:a1b2c3d4e567
'-w' 将两个字符串连接在一起,并在字符串的每个字符之间插入一个'*',像这样
combine_strings-w 请输入最多30个字符的字符串:abcde 请输入最多30个字符的字符串:1234567 合并后的字符串为:abcde*1*2*3*4*5*6*7
这是我目前得到的。
#include<stdio.h>
#include<string.h>
#include<memory.h>
void alt_merge(char *string1,char *string2,char *stringfinal){
while (*string1 != '[=10=]' && *string2 != '[=10=]')
{
*stringfinal++= *string1++;
*stringfinal++ = *string2++;
}
while (*string1 != '[=10=]')
*stringfinal++=*string1++;
while (*string2 != '[=10=]')
*stringfinal++ = *string2++;
*stringfinal='[=10=]';
}
void concate_star(char *string1,char *string2,char *stringfinal){
strcpy(stringfinal,strcat(string1,string2));
}
int main(int argc,char *argv[]){
char *string1[30+1];
char *string2[30+1];
char *stringfinal=malloc(strlen(string1)+strlen(string2)+1);
if(argv[1]=='-i'){
printf("Please enter a string of maximum 30 characters: ");
scanf("%s",&string1);
printf("Please enter a string of maximum 30 characters: ");
scanf("%s",&string2);
alt_merge(string1,string2,stringfinal);
printf("The combined string is: %s",stringfinal);
}
else if(argv[1]=='-w'){
printf("Please enter a string of maximum 30 characters: ");
scanf("%s",&string1);
printf("Please enter a string of maximum 30 characters: ");
scanf("%s",&string2);
concate_star(*string1,*string2,*stringfinal);
printf("The combined string is: %s",stringfinal);
}
return 0;
}
我收到这些错误消息。
jdoodle.c: In function 'main':
jdoodle.c:27:23: warning: implicit declaration of function 'malloc' [-Wimplicit-function-declaration]
char *stringfinal=malloc(strlen(string1)+strlen(string2)+1);
^~~~~~
jdoodle.c:27:23: warning: incompatible implicit declaration of built-in function 'malloc'
jdoodle.c:27:23: note: include '<stdlib.h>' or provide a declaration of 'malloc'
jdoodle.c:27:37: warning: passing argument 1 of 'strlen' from incompatible pointer type [-Wincompatible-pointer-types]
char *stringfinal=malloc(strlen(string1)+strlen(string2)+1);
^~~~~~~
In file included from jdoodle.c:2:0:
/usr/include/string.h:384:15: note: expected 'const char *' but argument is of type 'char **'
extern size_t strlen (const char *__s)
^~~~~~
jdoodle.c:27:53: warning: passing argument 1 of 'strlen' from incompatible pointer type [-Wincompatible-pointer-types]
char *stringfinal=malloc(strlen(string1)+strlen(string2)+1);
^~~~~~~
In file included from jdoodle.c:2:0:
/usr/include/string.h:384:15: note: expected 'const char *' but argument is of type 'char **'
extern size_t strlen (const char *__s)
^~~~~~
jdoodle.c:29:17: warning: multi-character character constant [-Wmultichar]
if(argv[1]=='-i'){
^~~~
jdoodle.c:29:15: warning: comparison between pointer and integer
if(argv[1]=='-i'){
^~
jdoodle.c:35:23: warning: passing argument 1 of 'alt_merge' from incompatible pointer type [-Wincompatible-pointer-types]
alt_merge(string1,string2,stringfinal);
^~~~~~~
jdoodle.c:5:6: note: expected 'char *' but argument is of type 'char **'
void alt_merge(char *string1,char *string2,char *stringfinal){
^~~~~~~~~
jdoodle.c:35:31: warning: passing argument 2 of 'alt_merge' from incompatible pointer type [-Wincompatible-pointer-types]
alt_merge(string1,string2,stringfinal);
^~~~~~~
jdoodle.c:5:6: note: expected 'char *' but argument is of type 'char **'
void alt_merge(char *string1,char *string2,char *stringfinal){
^~~~~~~~~
jdoodle.c:39:22: warning: multi-character character constant [-Wmultichar]
else if(argv[1]=='-w'){
^~~~
jdoodle.c:39:20: warning: comparison between pointer and integer
else if(argv[1]=='-w'){
^~
jdoodle.c:45:44: warning: passing argument 3 of 'concate_star' makes pointer from integer without a cast [-Wint-conversion]
concate_star(*string1,*string2,*stringfinal);
^
jdoodle.c:19:6: note: expected 'char *' but argument is of type 'char'
void concate_star(char *string1,char *string2,char *stringfinal){
^~~~~~~~~~~~
我是编程新手,非常感谢任何帮助和建议。
两个问题,你没有在函数malloc()
的声明处包含stdlib.h
。
此外,您向 strlen()
传递了一个错误的参数,但您传递了 char**
。其他功能也一样。您的参数不匹配。
char *string1[30+1];
char *string2[30+1];
你想要
char string1[30+1];
char string2[30+1];
在比较 "-i"
和 argv[1]
时,您还应该使用 strcmp
。
现在 argv
基本上是 char*
的数组。您基本上是在比较空终止字符数组和 (string) 与 "-i"
。 (不是 '-i'
)。
还注意到一件有趣的事情,
编译器说
jdoodle.c:29:15: warning: comparison between pointer and integer
if(argv[1]=='-i'){
^~
An ordinary character literal that contains more than one c-char is a multicharacter literal . A multicharacter literal has type int and implementation-defined value.
'-i'
是多字符文字。这有类型 int
这就是 pointer
和 int
.
还有一件事你应该清楚地知道,当你将数组传递给函数时,它会衰减为指向数组第一个元素的指针。
char *s[100]
衰减为 char**
因为数组的内容是 char*
指向它的指针将是 char**
.