gpt4 book ai didi

c - 根据命令行参数将字符串合并在一起。

转载 作者:行者123 更新时间:2023-11-30 16:38:00 25 4
gpt4 key购买 nike

我正在尝试用 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 != '\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){
^~~~~~~~~~~~

我是编程新手,因此非常感谢您的帮助和建议。

最佳答案

有两个问题,您没有包含 stdlib.h ,其中有函数 malloc() 的声明。

此外,您向 strlen() 传递了错误的参数,但传递了 char**。其他功能也一样。您的参数不匹配。

char *string1[30+1];
char *string2[30+1];

你想要

char string1[30+1];
char string2[30+1];

此外,当您将 "-i"argv[1] 进行比较时,您应该使用 strcmp

现在 argv 基本上是 char* 数组。您基本上是将空终止字符数组与(字符串)与“-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' 是多字 rune 字。它的类型为 int,这就是为什么 pointerint 之间进行警告比较的原因。

还有一件事您应该清楚地知道,当您将数组传递给函数时,它会衰减为指向数组第一个元素的指针。

char *s[100] 衰减为 char** 因为数组的内容是 char* 指向它的指针将是字符**

关于c - 根据命令行参数将字符串合并在一起。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47645260/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com