0% found this document useful (0 votes)
22 views

String

This document contains C code implementations of common string handling functions: strlen(), strcpy(), strrev(), strcmp(), and strcat(). Each function is defined in its own section with a short main() function to test it. Strlen() returns the length of a string. Strcpy() copies one string to another. Strrev() reverses a string. Strcmp() compares two strings and returns 0 if equal. Strcat() concatenates two strings.

Uploaded by

Arijit Sen Gupta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

String

This document contains C code implementations of common string handling functions: strlen(), strcpy(), strrev(), strcmp(), and strcat(). Each function is defined in its own section with a short main() function to test it. Strlen() returns the length of a string. Strcpy() copies one string to another. Strrev() reverses a string. Strcmp() compares two strings and returns 0 if equal. Strcat() concatenates two strings.

Uploaded by

Arijit Sen Gupta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

/*strlen()*/

#include<stdio.h>
#include<conio.h>
int slen(char b[])
{
int i=0,len=0;
while(b[i]!='\0')
{
i++;
len++;
}
return len;
}
void main()
{
char a[100];
int l;
clrscr();
puts("Enter your string..");
gets(a);
l=slen(a);
printf("% d",l);
getch();
}

----------------------------------------------------------------------------------------------------------------

/*strcpy()*/

#include<stdio.h>
#include<conio.h>
void scpy(char target[], char source[])
{
int i=0,len=0;
while(source[i]!='\0')
{
target[i]=source[i];
i++;
}
target[i]='\0';
}
void main()
{
char a[100],b[100];
clrscr();
puts("Enter your string..");
gets(a);
scpy(b,a);
puts(a);
puts(b);
getch();
}
/*strrev()*/

#include<stdio.h>
#include<conio.h>
void srev(char b[])
{
int i=0,len=0;
char temp;
while(b[i]!='\0')
{
i++;
len++;
}
for(i=0;i<len/2;i++)
{
temp=b[i];
b[i]=b[len-1-i];
b[len-1-i]=temp;
}
}
void main()
{
char a[100];
clrscr();
puts("Enter your string..");
gets(a);
srev(a);
puts(a);
getch();
}

----------------------------------------------------------------------------------------------------------------

/*strcmp()*/

#include<stdio.h>
#include<conio.h>
int scmp(char s1[], char s2[])
{
int i=0;
while(s1[i]==s2[i])
{
if(s1[i]=='\0')
return 0;
i++;
}
return (s1[i]-s2[i]);
}
void main()
{
char a[100],b[100];
int value;
clrscr();
puts("Enter 1st string..");
gets(a);
puts("Enter 2nd string..");
gets(b);
value=scmp(a,b);
if(value==0)
puts("Same!");
else
puts("Different!");
getch();
}

----------------------------------------------------------------------------------------------------------------

/*strcat()*/

#include<stdio.h>
#include<conio.h>
void scat(char dest[], char source[])
{
int i=0, len2=0;
while(dest[i]!='\0')
{
i++;
len2++;
} //calculates the length of destination string
i=0;
while(source[i]!='\0')
{
dest[len2]=source[i];
i++;
len2++;
} //merging
dest[len2]='\0';
}
void main()
{
char a[100],b[100];
clrscr();
puts("Enter 1st string..");
gets(a);
puts("Enter 2nd string..");
gets(b);
scat(b,a);
puts(a);
puts(b);
getch();
}

You might also like