0
#include<stdio.h>
void astrcat(char s[], char t[]);  //to concatenate two string

main()
{
    int i=0,p=0,q=0,c;
    char s[100],t[100];
    while((c=getchar())!='\n')
    s[p++]=c;
    while((c=getchar())!='\n')
    t[q++]=c;
    astrcat(s,t);
    while(s[i]!='\0')
    printf("%c",s[i++]);
    return 0;
}
void astrcat(char s[],char t[])
{

       int i, j;

       i = j = 0;
       while (s[i] != '\0')       /* find end of s */
           i++;

       while ((s[i++] = t[j++]) != '\0') /* copy t */
           ;


}

The above code works fine but show some additional symbols after the concatenated string. I've checked it several times but am unable to spot the error. Pls run this code on ur system and let me know where lies the error.
Thanks in advance
R.S.V.P. :confused:

3
Contributors
3
Replies
4
Views
7 Years
Discussion Span
Last Post by nathanurag
Featured Replies
  • working now. check it. there were 3 errrors mentioned in comments in code [CODE] #include<stdio.h> void astrcat(char s[], char t[]); //to concatenate two string int main() { int i=0,p=0,q=0,c; char s[100],t[100]; while((c=getchar())!='\n') { s[p++]=c; } s[p] = '\0'; // error 1 put null after taking input while((c=getchar())!='\n') { t[q++]=c; } … Read More

2

working now. check it.
there were 3 errrors mentioned in comments in code

#include<stdio.h>
  
      void astrcat(char s[], char t[]); //to concatenate two string
      int main()
      {
  
      int i=0,p=0,q=0,c;
  
      char s[100],t[100];
  
      while((c=getchar())!='\n')
	  {
		s[p++]=c;
	  }
	  s[p] = '\0'; // error 1        put null after taking input
      while((c=getchar())!='\n')
	  {
		t[q++]=c;
	  }
	  t[q] = '\0';   // error 2        put null after taking input
      astrcat(s,t);
	  i = 0;         // error 3  again initialize i to 0 before printing
      while(s[i]!='\0')
      printf("%c",s[i++]);
      return 0;
      }
      void astrcat(char s[],char t[])
      {
      int i, j;
      i = j = 0;
      while (s[i] != '\0') /* find end of s */
      i++;
      while ((s[i++] = t[j++]) != '\0') /* copy t */
      ;
      }
This question has already been answered. Start a new discussion instead.
Be sure to adhere to our posting rules.