A
arnuld
PURPOSE: write a pointer version of strcat function.
WHAT I DID: I have 2 implementations of this function. Both compile
and run but 1st one gives some strange results, 2nd is ok. I am unable
to figure out why the 1st implementation gives strange results:
/* A rudimentary strcat function
*
*/
#include <stdio.h>
#include <stdlib.h>
enum MAXSIZE { ARR_SIZE = 1000 };
void my_strcat( char s[], char t[] );
int main()
{
char s[1000] = "Saurabh";
char t[1000] = "Nirkhey\n";
printf("\ns --> %s\nt --> %s\n\n", s, t );
printf("--- concatenating strings -----\n\n");
my_strcat( s, t );
printf("\ns --> %s\nt --> %s\n\n", s, t );
return EXIT_SUCCESS;
}
void my_strcat( char s[], char t[] )
{
char *ps, *pt;
ps = s;
pt = t;
while( *ps++ != '\0' )
{
printf("*ps = %c\n", *ps );
}
while( *pt != '\0' )
{
*ps++ = *pt++;
}
}
=========== OUTPUT ===============
/home/arnuld/programs/C $ gcc -ansi -pedantic -Wall -Wextra 5-3.c
/home/arnuld/programs/C $ ./a.out
s --> Saurabh
t --> Nirkhey
--- concatenating strings -----
*ps = a
*ps = u
*ps = r
*ps = a
*ps = b
*ps = h
*ps = ^@
==> 2nd version's strcat function:
void my_strcat( char s[], char t[] )
{
char *ps, *pt;
ps = s;
pt = t;
while( *ps != '\0' )
{
printf("*ps = %c\n", *ps );
ps++;
}
while( *pt != '\0' )
{
*ps++ = *pt++;
}
}
============ OUPUT ============
/home/arnuld/programs/C $ gcc -ansi -pedantic -Wall -Wextra 5-3.c
/home/arnuld/programs/C $ ./a.out
s --> Saurabh
t --> Nirkhey
--- concatenating strings -----
*ps = S
*ps = a
*ps = u
*ps = r
*ps = a
*ps = b
*ps = h
s --> SaurabhNirkhey
t --> Nirkhey
/home/arnuld/programs/C $
-- http://lispmachine.wordpress.com/
Please remove capital 'V's when you reply to me via e-mail.
WHAT I DID: I have 2 implementations of this function. Both compile
and run but 1st one gives some strange results, 2nd is ok. I am unable
to figure out why the 1st implementation gives strange results:
/* A rudimentary strcat function
*
*/
#include <stdio.h>
#include <stdlib.h>
enum MAXSIZE { ARR_SIZE = 1000 };
void my_strcat( char s[], char t[] );
int main()
{
char s[1000] = "Saurabh";
char t[1000] = "Nirkhey\n";
printf("\ns --> %s\nt --> %s\n\n", s, t );
printf("--- concatenating strings -----\n\n");
my_strcat( s, t );
printf("\ns --> %s\nt --> %s\n\n", s, t );
return EXIT_SUCCESS;
}
void my_strcat( char s[], char t[] )
{
char *ps, *pt;
ps = s;
pt = t;
while( *ps++ != '\0' )
{
printf("*ps = %c\n", *ps );
}
while( *pt != '\0' )
{
*ps++ = *pt++;
}
}
=========== OUTPUT ===============
/home/arnuld/programs/C $ gcc -ansi -pedantic -Wall -Wextra 5-3.c
/home/arnuld/programs/C $ ./a.out
s --> Saurabh
t --> Nirkhey
--- concatenating strings -----
*ps = a
*ps = u
*ps = r
*ps = a
*ps = b
*ps = h
*ps = ^@
==> 2nd version's strcat function:
void my_strcat( char s[], char t[] )
{
char *ps, *pt;
ps = s;
pt = t;
while( *ps != '\0' )
{
printf("*ps = %c\n", *ps );
ps++;
}
while( *pt != '\0' )
{
*ps++ = *pt++;
}
}
============ OUPUT ============
/home/arnuld/programs/C $ gcc -ansi -pedantic -Wall -Wextra 5-3.c
/home/arnuld/programs/C $ ./a.out
s --> Saurabh
t --> Nirkhey
--- concatenating strings -----
*ps = S
*ps = a
*ps = u
*ps = r
*ps = a
*ps = b
*ps = h
s --> SaurabhNirkhey
t --> Nirkhey
/home/arnuld/programs/C $
-- http://lispmachine.wordpress.com/
Please remove capital 'V's when you reply to me via e-mail.