Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
10 views

More Example String Handling Function

This document provides examples of using common C string handling functions including strcpy(), strcat(), strncat(), strlen(), and strcmp(). Strcpy() is used to copy one string to another. Strcat() concatenates two strings together. Strncat() concatenates a specified number of characters from one string to another. Strlen() returns the length of a string. Strcmp() compares two strings and returns 0 if they are equal.

Uploaded by

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

More Example String Handling Function

This document provides examples of using common C string handling functions including strcpy(), strcat(), strncat(), strlen(), and strcmp(). Strcpy() is used to copy one string to another. Strcat() concatenates two strings together. Strncat() concatenates a specified number of characters from one string to another. Strlen() returns the length of a string. Strcmp() compares two strings and returns 0 if they are equal.

Uploaded by

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

More example string handling function

strcpy()
/*Example of concate string */
#include<stdio.h>
int main()
{
char string1[50]= "C Programming is fun.";
char string2[40]= "Lets learn C programming language";
char string3[100];
strcpy(string3,strcat(string1,string2));
printf(string3);
}
strncat()
/*Example of concatenation string */
#include<stdio.h>
int main()
{
char string1[21];
char string2[21];
char string3[50];
int length;
printf("\nEnter a string of no more than 20 Characters:\n");
gets(string1);
printf("\nEnter another 20 Characters of a string:\n");
gets(string2);
printf("\nEnter an integer:\n");
scanf("%d,& length);
strcpy(string3,strncat(string1,string2, length));
printf("\nThe concatenation string is: %s", string3);
printf("\nIts length is: %d",strlen(string3));
}
strlen()
#include<stdio.h>
int main()
{
char inbuf[128];
int slen;
/* holds length of string */
while(1)
{
printf("Enter a string ");
gets(inbuf);
slen = strlen(inbuf);
/* find length */
if(slen == 0) break;
/* termination condition */
while(slen > 0)
{
slen--;
printf("%c",*(inbuf+slen));
}
printf("\n");
}
}

strcpy()
#include<stdio.h>int main()
{
char
x[22];
strcpy(x,"C Programming Example");
printf(This is a %s,x);
}

strcmp()
#include<stdio.h>
int main()
{
char x[22],*y;
strcpy(x,"A Programming Example");
y = x;
/*
First test - compare y with constant */
if( y == "A Programming Example")
printf("Equal 1\n");
else
printf("Unequal 1\n");
/*
Second test - compare using strcmp() */
if(strcmp(x,"A Programming Example") == 0)
printf("Equal 2\n");
else
printf("Unequal 2\n");
/*
Assign constant address and compare */
y = "A Programming Example";
if( y == "A Programming Example")
printf("Equal 3\n");
else
printf("Unequal 3\n");
}

You might also like