| Current Path : /var/www/html/bibhas.ghoshal/ITP_2019/lab/ |
| Current File : /var/www/html/bibhas.ghoshal/ITP_2019/lab/using_strings.c |
//
// using_strings.c
//
//
// Created by Bibhas Ghoshal on 20/02/21.
//
#include <stdio.h>
int strlen (const char *s) {
int n;
for (n=0; *s!='\0'; ++s)
++n;
return n;
}
char *strcat(char *s1, const char *s2)
{
char *p = s1;
while (*p != '\0') /* go to end */
++p;
while(*s2 != '\0')
*p++ = *s2++; /* copy */ *p = '\0';
return s1;
}
int strcmp(char *s1, const char *s2) {
for (;*s1!='\0'&&*s2!='\0'; s1++,s2++) {
if (*s1>*s2) return 1;
if (*s2>*s1) return -1;
}
if (*s1 != '\0') return 1;
if (*s2 != '\0') return -1;
return 0;
}
char * strcpy (char *s1, const char *s2) {
char *p = s1;
while (*p++ = *s2++) ;
return s1;
}
int main()
{
char s1[ ] = "beautiful big sky country",
s2[ ] = "how now brown cow"; printf("%d\n",strlen (s1));
printf("%d\n",strlen (s2+8));
printf("%d\n", strcmp(s1,s2));
printf("%s\n",s1+10);
strcpy(s1+10,s2+8);
strcat(s1,"s!");
printf("%s\n", s1);
return 0;
}