[英]concatenating (adding) two charcter strings
I was told to write a program containing a concatenate function.有人告诉我编写一个包含连接函数的程序。 This program should collect the input strings using
fgets (&s1[0], len1+1, stdin)
and then add the two to each other to produce a final product.该程序应使用
fgets (&s1[0], len1+1, stdin)
收集输入字符串,然后将两者相加以生成最终产品。
My problem falls in that the program compiles but it doesn't display anything on the screen whatsoever, here's what I've got.我的问题在于程序编译但它没有在屏幕上显示任何内容,这就是我所拥有的。 I couldn't see how I could get it solved without this method of approach.
如果没有这种方法,我不知道如何解决它。
//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;
}
aside from the mistake with the for loop that others have pointed out, the while loop in your str_len function is wrong.除了其他人指出的 for 循环错误之外, str_len 函数中的 while 循环是错误的。 you should've used while(s[i] != NULL) instead of s[i] = null.
你应该使用 while(s[i] != NULL) 而不是 s[i] = null。 one equal sign, "=", is assignment;
一个等号“=”是赋值; two equal signs, "==", is comparisons;
两个等号“==”是比较; and exclamation equals, "!=", means not equal.
感叹号等于“!=”,表示不等于。
Secondly, you reassign your s1 and s2 to different memory locations in your string_cat function with their first character as NULL, "\\0".其次,您将 s1 和 s2 重新分配到 string_cat 函数中的不同内存位置,它们的第一个字符为 NULL,“\\0”。 this will always give your str_len a length of 0 if corrected your str_len function as pointed out above, and a length of random number if not corrected based on what's occupying your memory at run time.
如果如上所述更正了您的 str_len 函数,这将始终为您的 str_len 提供长度为 0 的长度,如果未根据运行时占用您的内存的内容进行更正,则将始终为您提供一个随机数的长度。
thirdly [still in the string_cat function], your if(int x = 0; len1 + len2 < 80; \\0; doesn't make sense. you're not doing any concatenations in this function at all.第三 [仍在 string_cat 函数中],你的if(int x = 0; len1 + len2 < 80; \\0;没有意义。你根本没有在这个函数中做任何连接。
Sorry for not providing you with the solution as this is a simple exercise.很抱歉没有为您提供解决方案,因为这是一个简单的练习。 I feel like spoiling you if I were to provide you with the code.
如果我要向您提供代码,我想宠坏您。
In your code having lot of compilation error.在您的代码中有很多编译错误。 Copy paste the code what you have compiled.
复制粘贴你编译的代码。
Check this line of code检查这行代码
int i=0;
for (i; i !=0; ++i)
Because of this you are not getting any thing.正因为如此,你没有得到任何东西。 In for loop you have condition
i !=0
which always fail so it's not entering inside the loop.在 for 循环中,您有条件
i !=0
总是失败,因此它不会进入循环内。
First problem is here第一个问题在这里
int i=0;
for (i; i !=0; ++i)
You set value 0 to the variable i
, and then you check if it does not equal 0. This check does not obviosly pass because i
equals 0
.您将值 0 设置为变量
i
,然后检查它是否不等于 0。此检查显然不会通过,因为i
等于0
。
The second problem is also the loop.第二个问题也是循环。 I can't really get the reason you need the loop it at all, because
i
is not used at all, exept the increment.我根本无法理解您需要循环的原因,因为
i
根本没有使用,除了增量。 So as far as i get it, the loop is not needed at all.所以据我所知,根本不需要循环。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.