주어진 명령 줄 인수를 기반으로 두 가지 방법으로 두 문자열을 병합하려면 C로 프로그램을 작성하려고합니다. 1234567 결합 된 문자열은 다음과 같습니다 최대 30 문자열을 입력하십시오 ABCDE : 최대 30 문자열을 입력하십시오 -i이명령 줄 인수를 기반으로 문자열을 병합합니다.
combine_strings 같은 문자열 교류를 병합
'-i' : a1b2c3d4e567
'-w'는 함께 2 문자열을 concate, 삽입 할 수있는 '*'와 같은 문자열의 각 문자 사이의
combine_strings -w 최대 30 characte의 문자열을 입력하십시오 RS : 최대 30 개 문자의 문자열을 입력하세요 ABCDE : 1234567 조합 된 문자열은 : 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