Computing.Net > Forums > Programming > strncat and strncmp help in C.

strncat and strncmp help in C.

Reply to Message Icon

Original Message
Name: coolie
Date: May 8, 2003 at 10:30:10 Pacific
Subject: strncat and strncmp help in C.
OS: xp
CPU/Ram: 256
Comment:

Hi,
I ve question about strncat and strncmp. How does it work if we use pointer notition.
Suppose we declare the function as :
char *strncat ( char *string, char *suffix, int n ) ;

and same for strncmp how does it work. i tried many ways but it gives error.
Can anyone help me.
if we call from main we cant use string.h
as in string.h it is previously declared.
So can anyone help me by writing the solution or by giving some hints.


Report Offensive Message For Removal


Response Number 1
Name: zzray
Date: May 8, 2003 at 19:36:51 Pacific
Subject: strncat and strncmp help in C.
Reply: (edit)

hi,

do you want to use the function that is defined in string.h or do you want to write your own.
if you are using the standard library then
the declaration for strncat is
char *strncat(char *s1, const char *s2, size_t n);

so you just declare two strings
char s1[10] ="abc", s2[10]="def";
alternatively
char *s1, *s2;
s1 = (char*)malloc(sizeof (char)*10);
s2 = (char*)malloc(sizeof (char)*10);

then strncat(s1,s2,2); will modify
s1 to "abcde".
strncmp works pretty much the same way.

zzray



Report Offensive Follow Up For Removal

Response Number 2
Name: Someone
Date: May 8, 2003 at 20:27:52 Pacific
Subject: strncat and strncmp help in C.
Reply: (edit)

Hi,
thanks for the reply. i want the strncat and strncpy using pointer to the function. i can send the solution for strncpy and the same i want with strncat and strncpy.

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
#include
//#include
#include

char *strncpy ( char *s1, char *s2, int n );
main()
{

char s1[15] = "the string is";
char s2[30] = "the string is one";

int k1[1] ;

strncpy( s1, s2,6 );
strncpy( s1, s2,6 );
strncpy( s1, s2,6 );
strncpy( s1, s2,6 );


return 0 ;

}

char *strncpy ( char *s1, char *s2, int n )
{

int i = 0 ;

while ( i n )
{

s1[i] = s2[i] ;
i++;

}

s1[i] = '\0';

printf("The string has been copied%s\n", s1);

return 0 ;

}


Report Offensive Follow Up For Removal

Response Number 3
Name: zzray
Date: May 8, 2003 at 22:39:03 Pacific
Subject: strncat and strncmp help in C.
Reply: (edit)

hi,

i guess strncat would look like this.

char *strncat ( char *s1, char *s2, int n )
{

int i = 0 ;
char* last = s1;
while( *last != '\0' )
last++;
while ( i n )
{

*last = s2[i] ;
i++;
last++;
}

*last = '\0';

printf("The string has been copied %s\n", s1);

return 0 ;
}

i understand that you want to use pointer to a function right. you can define a prototype first something like this

typedef char* (*strprocess)( char *s1, char *s2, int n );

then you can just create

stringprocess stringncopy = &strncpy;

then call it normally using it. or you can create an array of pointers for functions having the same prototype like in this case.

hope this was what you were looking for.

zzray


Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: strncat and strncmp help in C.

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




How often do you use Computing.Net?

Every Day
Once a Week
Once a Month
This Is My First Time!


View Results

Poll Finishes In 3 Days.
Discuss in The Lounge