私は、指定されたコマンドライン引数に基づいて2つの異なる方法で2つの文字列を一緒にマージするためにC言語でプログラムを書こうとしています。 1234567 組み合わせた文字列は次のとおりです。最大30文字の文字列を入力してください ABCDE: は最大30文字の文字列を入力してください-iこのコマンドライン引数に基づいて文字列をまとめてマージする。
combine_stringsのように、文字列の交流をマージする
「-i」 :a1b2c3d4e567
「-w」は一緒に2つの文字列をconcate、および挿入するには「*」のような文字列の各文字の間にこの
combine_strings -w 最大30 characteの文字列を入力してください。 RS:1234567 合成文字列がある: 最大30文字の文字列を入力してくださいABCDE B C D E * 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 != '\0' && *string2 != '\0')
{
*stringfinal++= *string1++;
*stringfinal++ = *string2++;
}
while (*string1 != '\0')
*stringfinal++=*string1++;
while (*string2 != '\0')
*stringfinal++ = *string2++;
*stringfinal='\0';
}
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){
^~~~~~~~~~~~
私はプログラミングに慣れていないので、何か助けと助言をいただければ幸いです。
クイック返信と明確な説明をありがとう。あなたのアドバイスを実装し、すべてが今働きます。 – Wilson