Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 连接(添加)两个字符字符串_C_Char_Concatenation_Fgets - Fatal编程技术网

C 连接(添加)两个字符字符串

C 连接(添加)两个字符字符串,c,char,concatenation,fgets,C,Char,Concatenation,Fgets,我被告知写一个包含连接函数的程序。此程序应使用fgets(&s1[0],len1+1,stdin)收集输入字符串 然后将二者相互添加,以生成最终产品 我的问题在于程序可以编译,但它不会在屏幕上显示任何东西,这是我得到的。如果没有这种方法,我看不出我怎么能解决这个问题 //function to terminate the program incase reach of 0 int str_len (char s[]) { int i=0; while (s[i]= NULL)

我被告知写一个包含连接函数的程序。此程序应使用
fgets(&s1[0],len1+1,stdin)收集输入字符串

然后将二者相互添加,以生成最终产品

我的问题在于程序可以编译,但它不会在屏幕上显示任何东西,这是我得到的。如果没有这种方法,我看不出我怎么能解决这个问题

//function to terminate the program incase reach of 0
int str_len (char s[])
{          
int i=0;
while (s[i]= NULL)
++i;
return i+1;
}
char string_cat (char*s1, char*s2)
{
//ADDING THE TWO STRINGS
int str_len(char s[])
char *s1 [80]= {'\0'};
char *s2 [40]= {'\0'};
int len1=str_len(s1);
int  len2=str_len(s2);
if (int x=0; len1+len2<80; \0;
return;
}
int main ()
{
char string_cat(char*s1,char*s2)
int str_len(char s[])
//RECIVING THE STRINGS TO ADD
char s1 [80];
char s2 [40];
int i=0;
for (i; i !=0; ++i)
{
printf("What is the first sentence?: ")
fgets(*s1[0], 75+1, stdin);
printf("What is the second sentence?:")
fgets(*s2[0],35+1,stdin);
string_cat(*s1,*s2);
printf("The two sentences added together produce the following: %c",s1 )
}
++i
return 0;
}
//用于在到达0时终止程序的函数
int str_len(字符s[]
{          
int i=0;
而(s[i]=NULL)
++一,;
返回i+1;
}
字符字符串(字符*s1,字符*s2)
{
//添加两个字符串
int str_len(字符s[]
char*s1[80]={'\0'};
char*s2[40]={'\0'};
int len1=str_len(s1);
int len2=str_len(s2);

如果(int x=0;len1+len2在你的代码中有很多编译错误。复制粘贴你编译的代码

检查这行代码

int i=0;
for (i; i !=0; ++i)
正因为如此,你得不到任何东西。在for循环中,你有条件
i!=0
,它总是失败,所以它不会进入循环中。

第一个问题在这里

int i=0;
for (i; i !=0; ++i)
将值0设置为变量
i
,然后检查它是否不等于0。此检查不会明显通过,因为
i
等于
0


第二个问题也是循环。我根本不知道你需要循环的原因,因为除了增量之外,根本没有使用
I
。就我所知,循环根本不需要。

除了其他人指出的for循环的错误之外,stru len函数中的while循环是错误的。 您应该使用while(s[i]!=NULL)而不是s[i]=NULL。一个等号“=”表示赋值;两个等号“==”,表示比较;感叹号equals“!=”表示不相等

其次,将s1和s2重新分配给string_cat函数中的不同内存位置,其第一个字符为NULL“\0”。如果如上所述更正str_len函数,则始终为str_len指定长度为0;如果未根据运行时占用内存的内容进行更正,则为随机数指定长度

第三[仍然在string_cat函数中],你的if(int x=0;len1+len2<80;\0;没有意义。你根本没有在这个函数中进行任何连接


很抱歉,没有为您提供解决方案,因为这只是一个简单的练习。如果我向您提供代码,我会很想破坏您。

您的代码无法编译。当我尝试编译它时,会出现大量错误。您使用了什么来编译它?在主函数中,您在传递变量时没有先声明它。我使用了代码块。也许这就是原因。/编辑:我打开了另一个程序。。这就是为什么,我现在得到了错误。。回到绘图板如果你想让其他人阅读你的问题,开始编辑它并修复缩进。“输入字符串应该使用fgets(&s1[0],len1+1,stdin)从键盘按顺序收集和fgets(&s2[0],len2+1,stdin),其中,len1和len2是各自输入字符串的最大大小“这会清除一点。据我所知,您根本不需要for(i;i!=0;++i)。正如我所说的,它没有使用,也不需要更多