Friday, 13 December 2013
Monday, 11 November 2013
Sunday, 6 October 2013
custom strcpy function in c to copy a string
#include<stdio.h>
#include<conio.h>
void mystrcpy(char *dest,const char*src)
{
while(*src)
{
*dest=*src;
src++;
dest++;
}
*dest='\0';
}
int main()
{
char str1[10]="Welcome";
char str2[10];
clrscr();
printf("\nEnter a string: ");
gets(str2);
puts(str1);
puts(str2);
//stcpy(str1,str2); -- way to call library function
mystrcpy(str1,str2);
puts(str1);
puts(str2);
return 0;
}
#include<conio.h>
void mystrcpy(char *dest,const char*src)
{
while(*src)
{
*dest=*src;
src++;
dest++;
}
*dest='\0';
}
int main()
{
char str1[10]="Welcome";
char str2[10];
clrscr();
printf("\nEnter a string: ");
gets(str2);
puts(str1);
puts(str2);
//stcpy(str1,str2); -- way to call library function
mystrcpy(str1,str2);
puts(str1);
puts(str2);
return 0;
}
custom strcmp function in c to compare two strings
#include<stdio.h>
#include<string.h>
#include<conio.h>
int mystrcmp(const char*s1,const char*s2)
{
int r=0;
while(*s1!='\0'||*s2!='\0')
{
if(*s1!=*s2)
{
r=(int)(*s1-*s2);
return r;
}
s1++;
s2++;
}
return r;
}
int main()
{
char s1[10]="Hello";
char s2[10]="HeLlo";
int d;
clrscr();
puts(s1);
puts(s2);
//d=strcmp(s1,s2);
d=mystrcmp(s1,s2);
printf("\nDiff is:%d",d);
getch();
return 0;
}
#include<string.h>
#include<conio.h>
int mystrcmp(const char*s1,const char*s2)
{
int r=0;
while(*s1!='\0'||*s2!='\0')
{
if(*s1!=*s2)
{
r=(int)(*s1-*s2);
return r;
}
s1++;
s2++;
}
return r;
}
int main()
{
char s1[10]="Hello";
char s2[10]="HeLlo";
int d;
clrscr();
puts(s1);
puts(s2);
//d=strcmp(s1,s2);
d=mystrcmp(s1,s2);
printf("\nDiff is:%d",d);
getch();
return 0;
}
C program to find length of the string using custom strlen function
#include<stdio.h>
#include<conio.h>
int main()
{
char src[10];
int l;
int mystrlen(const char*);
clrscr();
printf("\nEnter a string: ");
gets(src);
//l=strlen(src);
l=mystrlen(src);
printf("\nString lenght:%d",l);
getch();
return 0;
}
int mystrlen(const char*src)
{
int c=0;
while(*src!='\0')
{
++c;
src++;
}
return c;
}
Subscribe to:
Posts (Atom)
