简体   繁体   中英

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.

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. you should've used while(s[i] != NULL) instead of 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". 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.

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.

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.

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 .

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. So as far as i get it, the loop is not needed at all.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM