Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 507
  • Last Modified:

String Operation - C

How to do string concat without using strcat() ?

main()
{
     
    char s1[] =   "John";
    char s2[30] = "Kohn";
   
   
   
    int j = strlen(s2);
        xconcat(s2,s1);
printf( "\n String Output :: %s \n",s2);  //   o/p : KohnJohn

}

xconcat(char *s2, char *s1)
 
  {
   
     
             
             
  }
0
gauravflame
Asked:
gauravflame
  • 9
  • 9
  • 2
  • +4
3 Solutions
 
divyeshhdoshiCommented:
main()
{
     
    char s1[] =   "John";
    char s2[30] = "Kohn";
   
   
   
    int j = strlen(s2);
      char s[]=  xconcat(s2,s1);
printf( "\n String Output :: %s \n is %s",s2,s);  //   o/p : KohnJohn

}

string xconcat(char *s2, char *s1)
 
  {
   returun strcat(s1,s1);
               
             
  }
Regards,

Divyesh Doshi
0
 
gauravflameAuthor Commented:
Don't use "strcat" function provided by C
0
 
gauravflameAuthor Commented:
I tried this thing .. not get output


xconcat(char *s2, char *s1)
 
  {
     char *temp;
     
     while(len-- > 0)
     {
             
       temp = *s2++;
       
       if(*s2 == '\0')
       {
                       
             while(*s1 != '\0')
             {
                   temp = temp + *s1;
                   *s1++;
             }
       
       *s2 = temp;
       
       }
             
             
     }          
     
             
             
  }
         
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Infinity08Commented:
You can just copy the first string to the end of the second string. Can you use strcpy ?
0
 
grg99Commented:
try this:

void myconcat( char * d, char * s ) {

while( *d++ );   /* skip to end of destination */

while( *d++ = *s++ );  /* copy src to dest */

}
0
 
itsmeandnobodyelseCommented:
>>>> xconcat(s2,s1);
The xconcat isn't well defined as it has no return type and as it doesn't provide a buffer where the concatination can be made into. Note, strcat simply adds to the first argument string ... and doesn't care for sizes. That makes it extremely unsafe and error-prone.

To make a better concatination I would use a prototype

   char * xconcat(char * s, int size, const char * s1, const char * s2);

where the first argument is a (writeable) buffer that receives the concatened string.

An implementation could be made like that:

   char * xconcat(char * s, int size, const char * s1, const char * s2)
   {
          int l1 = strlen(s1);
          int l2 = strlen(s2);
          if (l1 < size)
              memmove(s, s1, l1);
          if (l1 + l2 + 1 < size)
              memmove(&s[l1], s2, l2+1);
          else
                 return NULL;
          return s;    
   }

Note, I used memove in case any of s1, s2 points to the same storage as s.

Regards, Alex
0
 
PaulCaswellCommented:
If you are allowed to use strcpy you can do:

char * strcatTheLessEasyWay ( char * str, char * cat )
{
  return strcpy(&str[strlen(str)], cat);
}
 
0
 
gauravflameAuthor Commented:

// Doesn't work .. not copying source into the end of Dest string

void myconcat( char * d, char * s ) {

while( *d++ );   /* skip to end of destination */

while( *d++ = *s++ );  /* copy src to dest */

}

0
 
gauravflameAuthor Commented:
I am using below code

char * strcatTheLessEasyWay ( char * str, char * cat )
{
  return strcpy(&str[strlen(str)], cat);
}
 

in below way

main()
{
 
 
   char str1[]   = "World";
   char str2[] = "Hello";
   
   char *p;
   char *myconcat(char,char);  // need to define properly
   p = myconcat(str2,str1);
   
   printf("\n String Str2 Target :: %s \n", *p);
}

 char * myconcat ( char * str, char * cat )
{
  return strcpy(&str[strlen(str)], cat);
}

There is error in above code also



0
 
ozoCommented:
char str2[] = "Hello";
does not allocate space in which to a[[emd anything else

char *myconcat(char,char);  // need to define properly
should be
char *myconcat(char *,char *);
0
 
ozoCommented:
 p = myconcat(str2,str1);
   printf("\n String Str2 Target :: %s \n", *p);
   

If you wanted p to be str2 after the append, then you might do
   strcpy(&str[strlen(str)], cat);
   return str;
0
 
gauravflameAuthor Commented:
// Doesn't Work

main()
{
 char str1[]   = "World";
   char str2[] = "Hello";
   
   char *p;
   char *myconcat(char *,char *);

   p = myconcat(str2,str1);
   
   printf("\n String Str2 Target :: %s \n", *p);
   
}

char * myconcat ( char * str, char * cat )
{
  return strcpy(&str[strlen(str)], cat);
}
0
 
ozoCommented:
char str2[] = "Hello";
does not allocate space in which to append anything else
0
 
ozoCommented:
and even if it did, the return value of
return strcpy(&str[strlen(str)], cat);
would point to just the part that was appended
0
 
gauravflameAuthor Commented:
I make it

char str2[30] = "Hello";

Still not working...
0
 
ozoCommented:
Define "not working"
0
 
ozoCommented:
this is wrong
printf("\n String Str2 Target :: %s \n", *p);
*p is the character pointed to by p
%s wants a pointer, to a character string, which woud be
printf("\n String Str2 Target :: %s \n", p);
or
printf("\n String Str2 Target :: %s \n", str2);
0
 
gauravflameAuthor Commented:
its working
main()
{
char str1[]   = "World";
   char str2[30] = "Hello";
   
   char *p;
   char *myconcat(char *,char *);

   myconcat(str2,str1);
   
   printf("\n String Str2 Target :: %s \n", str2);
   
}

 char * myconcat ( char * str, char * cat )
{
  return strcpy(&str[strlen(str)], cat);
}


doesn't work if I do

char *p;
   char *myconcat(char *,char *);

   p = myconcat(str2,str1);
   
   printf("\n Target String ::\n", *p);


o/p getting :: Target String ::     // getting blank o/p

==========================

Using below code

p = myconcat(str2,str1);
   
   //printf("\n Target String ::\n", *p);
   
   while( *p != '\0')
   {
           
     printf("%c",*p);
     p++;        
           
    }      


I am getting o/p :: World                      // not desired o/p HelloWorld


0
 
itsmeandnobodyelseCommented:
>>>> char * myconcat ( char * str, char * cat )
make the second a const char *

>>>> return strcpy(&str[strlen(str)], cat);
returns only the second string. You need

    strcpy(&str[strlen(str)], cat);
    return str;    

>>>> *p
check what ozo had posted.

>>>> main()

you need
 
    int main()

and  should have

    return 0;

as final statement.

Regards, Alex

0
 
ozoCommented:
If you want p = myconcat(str2,str1); to point to str2, then
do
char * myconcat ( char * str, char * cat )
{
  strcpy(&str[strlen(str)], cat);
  return str;
}
instead of
char * myconcat ( char * str, char * cat )
{
  return strcpy(&str[strlen(str)], cat);
}
0
 
gauravflameAuthor Commented:
Sorry for above code ... rephrasing

doesn't work if I do

char *p;
   char *myconcat(char *,char *);

   p = myconcat(str2,str1);
   
   printf("\n Target String :: %s \n", p);


o/p getting :: Target String :: World    // getting World not ...Hello World
0
 
ozoCommented:
printf("\n Target String ::\n", *p);
*p is a char
p is the pointer
but the printf format string has no % conversion specifiers anyway, so it doesn't even look at the *p
the format specifier to fetch a character argument would be %c
as in
 printf("\n Target character :: %c\n", *p);
0
 
gauravflameAuthor Commented:
Below code works if I like to go with

char *p;

print out the p;

char * myconcat ( char * str, char * cat )
{
  strcpy(&str[strlen(str)], cat);
  return str;
}
instead of
char * myconcat ( char * str, char * cat )
{
  return strcpy(&str[strlen(str)], cat);
}
0
 
ozoCommented:
return strcpy(&str[strlen(str)], cat);
returns the &str[strlen(str)] that was passed to strcpy
if you want to return str instead,
   strcpy(&str[strlen(str)], cat);
   return str;
0
 
PaulCaswellCommented:
Thanks for the fix ozo. :)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 9
  • 9
  • 2
  • +4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now