what is wrong with this function which acts like strncpy
Code:
#include <stdio.h>
#include <string.h>
#include <conio.h>
void xstrncpy(char *s, int n);
int main()
{
int n;
char s[80];
printf("\nEnter the string\n");
scanf("%s",s);
printf("\nEnter the number of characters to be copied");
scanf("%d",n);
xstrcpy(s,n);
getch();
return 0;
}
void xstrncpy(char *s, int n)
{
int i;
char s1[80], *s2;
s2=s1;
for(i=1;i<=n;i++)
{
*s2=*s;
s2++;
s++;
}
*s2='\0';
printf("S1 = %s",s1);
}
and this which is for strset
Code:
#include <stdio.h>
#include <string.h>
void xstrset(char *s, char c);
int main()
{
char s[]="apocalypse";
char c='a';
xstrset(s,c);
return 0;
}
void xstrset(char *s, char c)
{
while(*s)
{
*s=c;
s++;
}
printf("%s",s);
}
and this which reverses and string
Code:
#include <stdio.h>
#include <string.h>
void xstrrev(char *s, char *s1);
int main()
{
char s[]="sasdh";
char s1[10];
xstrrev(s,s1);
return 0;
}
void xstrrev(char *s, char *s1)
{
int len,i;
len=strlen(s);
while(*s!='\0')
{
s++;
}
while(len!=0)
{
s--;
*s1=*s;
len--;
}
*s1='\0';
printf("%s",s1);
}
Also i want to write a custom function for strstr. Can anyone pls help me for this?