0% found this document useful (0 votes)
31 views13 pages

C Program Examples

The document contains C program code examples for custom implementations of 15 common string handling functions from the string.h library in C: strlen, strlwr, strupr, strcat, strncat, strcpy, strncpy, strcmp, strncmp, strcmpi, stricmp, strdup, strchr, strrchr. Each code example includes the function code, a main function to test it, and sample output. The examples are intended to demonstrate how to write these string functions without using the standard library versions.

Uploaded by

Aayush Jha
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
31 views13 pages

C Program Examples

The document contains C program code examples for custom implementations of 15 common string handling functions from the string.h library in C: strlen, strlwr, strupr, strcat, strncat, strcpy, strncpy, strcmp, strncmp, strcmpi, stricmp, strdup, strchr, strrchr. Each code example includes the function code, a main function to test it, and sample output. The examples are intended to demonstrate how to write these string functions without using the standard library versions.

Uploaded by

Aayush Jha
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 13

C PROGRAM FOR CUSTOM FUNCTION OF ALL BUILT IN

FUNCTIONS AVAILABLE IN “string.h”


1.strlen
//strlen
#include<stdio.h>
#include<string.h>
int xstrlen(char *a);
int main()
{
char a[100];
printf("Enter the string:");
gets(a);
printf("Length of string is %d\n",xstrlen(&a));
return 0;
}
int xstrlen(char *a)
{ int l=0;
while(*a!='\0')
{
a++;
l++;

}
return l;
}

OUTPUT:
Enter the string:PROGRAMMING
Length of string is 11

2.strlwr
//strlwr
#include<string.h>
#include<stdio.h>
void xstrlwr(char *c);
int main()
{
char a[100];
int size;
printf("Enter the string:");
gets(a);
xstrlwr(&a);
printf("Lowercase:%s\n",a);
return 0;
}
void xstrlwr(char *c)
{ int b=0,i;
char d[100];
while(*(c+i)!='\0')
{
int b=*(c+i);
b=b+32;
d[i]=b;
*(c+i)=d[i];
i++;
}
}

OUTPUT:
Enter the string:COMPUTER
Lowercase:computer

3.strupr
//strupr
#include<string.h>
#include<stdio.h>
void xstrupr(char *c);
int main()
{
char a[100];
int size;
printf("Enter the string in lowercase:");
gets(a);
xstrupr(&a);
printf("UPPERCASE:%s\n",a);
return 0;
}
void xstrupr(char *c)
{ int b=0,i;
char d[100];
while(*(c+i)!='\0')
{
int b=*(c+i);
b=b-32;
d[i]=b;
*(c+i)=d[i];
i++;
}
}

OUTPUT:
Enter the string in lowercase:computer
UPPERCASE:COMPUTER

4.strcat
//strcat
#include<stdio.h>
#include<string.h>
void xstrcat(char *a,char *b);
int main()
{
char src[100];
char dest[100];
printf("Enter the first string:");
gets(src);
printf("Enter the second string:");
gets(dest);
xstrcat(&src,&dest);
printf("Concatenated string:%s\n",src);
return 0;
}
void xstrcat(char *a,char *b)
{ int i=0,j=0;
while(*(a+i)!='\0')
{
i++;
}
while(*(b+j)!='\0')
{
*(a+i+j)=*(b+j);
j++;
}
*(a+i+j)='\0';
}

OUTPUT:
Enter the first string:COMPUTER
Enter the second string:PROGRAMMING
Concatenated string:COMPUTERPROGRAMMING

5.strncat
//strncat
#include<stdio.h>
#include<string.h>
void xstrcat(char *a,char *b,int);
int main()
{
char src[100];
char dest[100];
int n;
printf("Enter the first string:");
gets(src);
printf("Enter the second string:");
gets(dest);
printf("Enter the number of character to concatenate:");
scanf("%d",&n);
xstrcat(&src,&dest,n);
printf("Concatenated string:%s\n",src);
return 0;
}
void xstrcat(char *a,char *b,int n)
{ int i=0,j=0;
while(*(a+i)!='\0')
{
i++;
}
while(*(b+j)!='\0' && j<n)
{
*(a+i+j)=*(b+j);
j++;
}
*(a+i+j)='\0';
}
OUTPUT:
Enter the first string:computer
Enter the second string:programming
Enter the number of character to concatenate:3
Concatenated string:computerpro

6.strcpy
//strcpy
#include<stdio.h>
#include<string.h>
void xstrcpy(char *a,char *b);
int main()
{
char a[100];
char b[100];
printf("Enter the source string:");
gets(a);
xstrcpy(&a,&b);
printf("Copied string:%s\n",b);
return 0;
}
void xstrcpy(char *a,char *b)
{ int i=0;
while(*(a+i)!=0)
{
*(b+i)=*(a+i);
i++;
}
}

OUTPUT:
Enter the source string:HELLO
HELLO

7.strncpy
//strncpy
#include<stdio.h>
#include<string.h>
void xstrncpy(char *a,char *b,int);
int main()
{
char a[100];
char b[100];
int n;
printf("Enter the source string:");
gets(a);

printf("Enter the number of string to be copied:");


scanf("%d",&n);
xstrncpy(&a,&b,n);
printf("Copied string:%s\n",b);
return 0;
}
void xstrncpy(char *a,char *b,int n)
{ int i=0;
while(*(a+i)!=0 && i<n)
{
*(b+i)=*(a+i);
i++;
}
}

OUTPUT:
Enter the source string:hello
Enter the number of string to be copied:2
Copied string:he

9.strcmp
//strcmp
#include<stdio.h>
#include<string.h>
int xstrcmp(char *a,char *b);
int main()
{
char a[100],b[100];
int result;
printf("Enter two strings:");
gets(a);
gets(b);
result=xstrcmp(&a,&b);
if(result==1) printf("Strings are not same\n");
else printf("Strings are same.\n");
return 0;
}
int xstrcmp(char *a,char *b)
{ int i=0;
while(*(a+i)!='\0')
{
if(*(a+i)==*(b+i)) i++;
else
{ return 1;
break;
}

OUTPUT:
Enter two strings:HELLO
HELLO
Strings are same.

10.strncmp
//strncmp
#include<stdio.h>
#include<string.h>
int xstrncmp(char *a,char *b,int);
int main()
{
char a[100],b[100];
int result,n;
printf("Enter two strings:");
gets(a);
gets(b);
printf("Enter the number of strings you want to compare:");
scanf("%d",&n);
result=xstrncmp(&a,&b,n);
if(result==1) printf("Strings are not same\n");
else printf("Strings are same.\n");
return 0;
}
int xstrncmp(char *a,char *b,int n)
{ int i=0;
while(*(a+i)!='\0' && i<n)
{
if(*(a+i)==*(b+i)) i++;
else
{ return 1;
break;
}

OUTPUT:
Enter two strings:hello
helinvin
Enter the number of strings you want to compare:3
Strings are same.

11.strcmpi

#include<stdio.h>
#include<string.h>
int xstrcmpi(char *a,char *b);
int main()
{
char a[100],b[100];
int result;
printf("Enter the first string:");
gets(a);
printf("Enter the second string:");
gets(b);
result=xstrcmpi(&a,&b);
if(result==1) printf("first string size is greater.\n");
else if(result==-1) printf("second string size is greater.\n");
else if(result==0) printf("Strings are same.\n");
return 0;
}
int xstrcmpi(char *a,char *b)
{
int i=0,j=0;
int asc_a,asc_b;
while(*(a+i)!='\0')
{
i++;
}
while(*(b+j)!='\0')
{
j++;
}
if(i>j) return 1;
else if(j>i) return -1;
else{
int i=0;
while(*(a+i)!='\0')
{
if(*(a+i)==*(b+i) || (*(a+i)+32)==*(b+i) ||
(*(a+i)-32)==*(b+i))
{
i++;
}
else
{
printf("Lengths are same but not string.\n");
break;
}
if(i==j) return 0;
}
}
}

OUTPUT:
Enter the first string:programming
Enter the second string:PROGRAMMING
Strings are same.
12.stricmp
//stricmp
#include<stdio.h>
#include<string.h>
int xstricmp(char *a,char *b);
int main()
{
char a[20],b[20];
int result;
printf("Enter the first string:");
gets(a);
printf("Enter the second string:");
gets(b);
result=xstricmp(&a,&b);
if(result==1) printf("first string size is greater.\n");
else if(result==-1) printf("second string size is greater.\n");
else if(result==0) printf("Strings are same.\n");
return 0;
}
int xstricmp(char *a,char *b)
{
int i=0,j=0;
int asc_a,asc_b;
while(*(a+i)!='\0')
{
i++;
}
while(*(b+j)!='\0')
{
j++;
}
if(i>j) return 1;
else if(j>i) return -1;
else{
int i=0;
while(*(a+i)!='\0')
{
if(*(a+i)==*(b+i) || (*(a+i)+32)==*(b+i) ||
(*(a+i)-32)==*(b+i))
{
i++;
}
else
{
printf("Lengths are same but not string.\n");
break;
}
if(i==j) return 0;
}
}
}

OUTPUT:
Enter the first string:integer
Enter the second string:float
first string size is greater.

13.strdup
#include<stdio.h>
#include<string.h>
void xstrdup(char *a,char *b);
int main()
{ int i=0;
char a[100],b[100];
printf("Enter the source string:");
gets(a);
xstrdup(&a,&b);
printf("Copied string:%s\n",b);

return 0;

}
void xstrdup(char *a,char *b)
{ int i=0;
while(*(a+i)!='\0')
{
*(b+i)=*(a+i);
i++;
}
}
OUTPUT:
Enter the source string:function
Copied string:function

14.strchr
#include<stdio.h>
#include<string.h>
void xstrchr(char *a,char b);
int main()
{
char a[100],b;
int x,i;
printf("Enter the string:");
gets(a);
printf("Enter the character:");
scanf("%c",&b);
xstrchr(&a,b);

return 0;

}
void xstrchr(char *a,char b)
{ int i=0;
while(*(a+i)!=b)
{
i++;
}
printf("String after %c is:",b);
while(*(a+i)!='\0')
{
printf("%c",*(a+i));
i++;
}
printf("\n");
}

OUTPUT:
Enter the string:engineering
Enter the character:i
String after i is:ineering
15.strrchr
//strrchr
#include<stdio.h>
#include<string.h>
void xstrrchr(char *a,char b);
int main()
{
char a[100],b;
int x,i;
printf("Enter the string:");
gets(a);
printf("Enter the character:");
scanf("%c",&b);
xstrrchr(&a,b);

return 0;

}
void xstrrchr(char *a,char b)
{ int i=0,c=0;
while(*(a+i)!='\0')
{
if(*(a+i)==b) c=i;
i++;
}
printf("String at last of %c is:",b);
for(int j=c;j<i;j++)
{
printf("%c",*(a+j));
}
printf("\n");
}

OUTPUT:
Enter the string:engineering
Enter the character:i
String at last of i is:ing

You might also like