#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:

Recommended Answers

All 3 Replies

Null terminate the strings you read in (s and t).

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 */
      ;
      }

thx a lot buddy for ur help !!!! :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.