Site pages
Current course
Participants
General
Module - 1 C Language Fundamentals
Module-2 Decision Making, Looping and Functions
Module 3 – Array, Structure and Pointers
Module-4 Data Structures
22 March - 28 March
Module 6. User define Functions, String Functions,...
Module 7. Passing arguments and returning values
Module 8. Recursion, scope and visibility of a var...
Module 9. Structures and union.
Module- 10 Pointers, Stacks, Push/Pop operations.
Lesson -9 Library Functions
9.1 INTRODUCTION
C provides a large set of standard library functions for specialized purposes. These may be divided into various categories. For instance
Character identification (ctype.h)
Mathematical functions (math.h)
String manipulation (string.h)
A program generally has to #include special header files in order to use special functions in libraries. The names of the appropriate files can be found in particular compiler manuals. In the examples above the names of the header files are given in parentheses
9.2 Character identification <ctype.h>
The ctype.h header file of the C Standard Library provides declares several functions useful for testing and mapping characters.
All the functions accepts int as a parameter, whose value must be EOF or representable as an unsigned char.
All the functions return non-zero (true) if the argument c satisfies the condition described, and zero if not.
9.2.1 Library Functions
Following are the functions defined in the header ctype.h:
S.N. |
Function & Description |
1 |
int isalnum(int c) |
2 |
int isalpha(int c) |
3 |
int iscntrl(int c) |
4 |
int isdigit(int c) |
5 |
int isgraph(int c) |
6 |
int islower(int c) |
7 |
int isprint(int c) |
8 |
int ispunct(int c) |
9 |
int isspace(int c) |
10 |
int isupper(int c) |
11 |
int isxdigit(int c) |
The library also contains two conversion functions that also accept and return an "int"
S.N. |
Function & Description |
1 |
int tolower(int c) |
2 |
int toupper(int c) |
9.2.2 isalnum()
Description: The C library function void isalnum(int c) checks if the passed character is alphanumeric.
Syntax: int isalnum(int c);
Parameters: c -- This is the character to be checked.
Return Value: This function returns nonzero value if c is a digit or a letter, else 0
The following example shows the usage of isalnum() function.
#include <stdio.h>
#include <ctype.h>
int main()
{
int var1 = 'd';
int var2 = '\t';
if( isalnum(var1) )
{
printf("var1 = |%c| is alphanumeric\n", var1 );
}
else
{
printf("var1 = |%c| is not alphanumeric\n", var1 );
}
if( isalnum(var2) )
{
printf("var2 = |%c| is alphanumeric\n", var2 );
}
else
{
printf("var2 = |%c| is not alphanumeric\n", var2 );
}
return(0);
}
Output:
var1 = |d| is alphanumeric
var2 = |\t| is not alphanumeric
9.2.3 isalpha ()
Description: The C library function void isalpha(int c) checks if the passed character is alphabetic.
Syntax: int isalpha(int c);
Parameters: c -- This is the character to be checked
Return Value: This function returns nonzero value if c is a alphabet, else 0
The following example shows the usage of isalpha() function.
#include <stdio.h>
#include <ctype.h>
int main()
{
int var1 = 'd';
int var2 = '2';
if( isalpha(var1) )
{
printf("var1 = |%c| is an alphabet\n", var1 );
}
else
{
printf("var1 = |%c| is not an alphabet\n", var1 );
}
if( isalpha(var2) )
{
printf("var2 = |%c| is an alphabet\n", var2 );
}
else
{
printf("var2 = |%c| is not an alphabet\n", var2 );
}
return(0);
}
Output:
var1 = |d| is an alphabet
var2 = |2| is not an alphabet
9.2.4 isdigit()
Description: The C library function void isdigit(int c) checks if the passed character is a decimal digit character.Decimal digits are(numbers): 0 1 2 3 4 5 6 7 8 9
Syntax: int isdigit(int c);
Parameters: c -- This is the character to be checked.
Return Value: This This function returns nonzero value if c is a digit, else 0
The following example shows the usage of isdigit() function.
#include <stdio.h>
#include <ctype.h>
int main()
{
int var1 = 'h';
int var2 = '2';
if( isdigit(var1) )
{
printf("var1 = |%c| is a digit\n", var1 );
}
else
{
printf("var1 = |%c| is not a digit\n", var1 );
}
if( isdigit(var2) )
{
printf("var2 = |%c| is a digit\n", var2 );
}
else
{
printf("var2 = |%c| is not a digit\n", var2 );
}
return(0);
}
Output:
var1 = |h| is not a digit
var2 = |2| is a digit
9.2.5 islower()
Description: The C library function int islower(int c) check whether the passed character is lowercase letter.
Syntax: int islower(int c);
Parameters: c -- This is the character to be checked.
Return Value: This function returns a non zero value(true) if c is a lowercase alphabetic letter else, zero (false)
The following example shows the usage of islower() function.
#include <stdio.h>
#include <ctype.h>
int main()
{
int var1 = 'Q';
int var2 = 'q';
if( islower(var1) )
{
printf("var1 = |%c| is lowercase character\n", var1 );
}
else
{
printf("var1 = |%c| is not lowercase character\n", var1 );
}
if( islower(var2) )
{
printf("var2 = |%c| is lowercase character\n", var2 );
}
else
{
printf("var2 = |%c| is not lowercase character\n", var2 );
}
return(0);
}
Let us compile and run the above program, this will produce the following result:
var1 = |Q| is not lowercase character
var2 = |q| is lowercase character
9.2.6 ispunct()
Description: The C library function int ispunct(int c) check whether the passed character is punctuation character.A punctuation character is any graphic character (as in isgraph) that is not alphanumeric (as in isalnum).
Syntax: int ispunct(int c);
Parameters: c -- This is the character to be checked.
Return Value: This function returns a non zero value(true) if c is a punctuation character else, zero (false)
The following example shows the usage of ispunct() function.
#include <stdio.h>
#include <ctype.h>
int main()
{
int var1 = 't';
int var2 = '/';
if( ispunct(var1) )
{
printf("var1 = |%c| is a punctuation character\n", var1 );
}
else
{
printf("var1 = |%c| is not a punctuation character\n", var1 );
}
if( ispunct(var2) )
{
printf("var2 = |%c| is a punctuation character\n", var2 );
}
else
{
printf("var2 = |%c| is not a punctuation character\n", var2 );
}
return(0);
}
Output:
var1 = |t| is not a punctuation character
var2 = |/| is a punctuation character
9.2.7 isspace()
-
Description:The C library function int isspace(int c) check whether the passed character is white-space.
-
Syntax: int isspace(int c);
-
Parameters: c -- This is the character to be checked.
-
Return Value: This function returns a non zero value(true) if c is a white-space character else, zero (false)
The following example shows the usage of isspace() function.
#include <stdio.h>
#include <ctype.h>
int main()
{
int var1 = 't';
int var2 = ' ';
if( isspace(var1) )
{
printf("var1 = |%c| is a white-space character\n", var1 );
}
else
{
printf("var1 = |%c| is not a white-space character\n", var1 );
}
if( isspace(var2) )
{
printf("var2 = |%c| is a white-space character\n", var2 );
}
else
{
printf("var2 = |%c| is not a white-space character\n", var2 );
}
return(0);
}
Output:
var1 = |t| is not a white-space character
var2 = | | is a white-space character
9.2.8 issupper()
Description: The C library function int isupper(int c) check whether the passed character is uppercase letter.
Declaration: Following is the declaration for isupper() function.
Syntax: int isupper(int c);
Parameters: c -- This is the character to be checked.
Return Value: This function returns a non zero value(true) if c is uppercase alphabetic letter else, zero (false)
The following example shows the usage of isupper() function.
#include <stdio.h>
#include <ctype.h>
int main()
{
int var1 = 'M';
int var2 = 'm';
if( isupper(var1) )
{
printf("var1 = |%c| is uppercase character\n", var1 );
}
else
{
printf("var1 = |%c| is not uppercase character\n", var1 );
}
if( isupper(var2) )
{
printf("var2 = |%c| is uppercase character\n", var2 );
}
else
{
printf("var2 = |%c| is not uppercase character\n", var2 );
}
return(0);
}
Output:
var1 = |M| is uppercase character
var2 = |m| is not uppercase character
9.2.9 isxdigit()
Description: The C library function int isxdigit(int c) check whether the passed character is hexadecimal digit.
Declaration: Following is the declaration for isxdigit() function.
Syntax: int isxdigit(int c);
Parameters: c -- This is the character to be checked.
Return Value: This function returns a non zero value(true) if c is hexadecimal digit else, zero (false)
The following example shows the usage of isxdigit() function.
#include <stdio.h>
#include <ctype.h>
int main()
{
char var1[] = "tuts";
char var2[] = "0xE";
if( isxdigit(var1[0]) )
{
printf("var1 = |%s| is hexadecimal character\n", var1 );
}
else
{
printf("var1 = |%s| is not hexadecimal character\n", var1 );
}
if( isxdigit(var2[0] ))
{
printf("var2 = |%s| is hexadecimal character\n", var2 );
}
else
{
printf("var2 = |%s| is not hexadecimal character\n", var2 );
}
return(0);
}
Output:
var1 = |tuts| is not hexadecimal character
var2 = |0xE| is hexadecimal character
9.2.10 tolower()
Description:The C library function int tolower(int c) converts a given letter to lowercase.
Declaration: Following is the declaration for tolower() function.
Syntax: int tolower(int c);
Parameters: c -- This is the letter to be converted to lowercase.
Return Value: This function returns lowercase equivalent to c, if such value exists, else c remain unchanged. The value is returned as an int value that can be implicitly casted to char.
The following example shows the usage of tolower() function.
#include <stdio.h>
#include <ctype.h>
int main()
{
int i = 0;
char c;
char str[] = "TUTORIALS POINT";
while( str[i] )
{
putchar(tolower(str[i]));
i++;
}
return(0);
}
Output:
tutorials point
9.2.11 toupper()
Description: The C library function int toupper(int c) converts lowercase letter to uppercase.
Declaration: Following is the declaration for toupper() function.
Syntax: int toupper(int c);
Parameters: c -- This is the letter to be converted to uppercase.
Return Value: This function returns uppercase equivalent to c, if such value exists, else c remain unchanged. The value is returned as an int value that can be implicitly casted to char.
The following example shows the usage of toupper() function.
#include <stdio.h>
#include <ctype.h>
int main()
{
int i = 0;
char c;
char str[] = "Tutorials Point";
while(str[i])
{
putchar (toupper(str[i]));
i++;
}
return(0);
}
Output:
TUTORIALS POINT
9.3 Mathematical functions <math.h>
The math.h header defines various mathematical functions and one macro. All the functions available in this library take double as an argument and return double as the result.
Following are the functions defined in the header math.h:
S.N. |
Function & Description |
1 |
double acos(double x) |
2 |
double asin(double x) |
3 |
double atan(double x) |
4 |
double atan2(doubly y, double x) |
5 |
double cos(double x) |
6 |
double cosh(double x) |
7 |
double sin(double x) |
8 |
double sinh(double x) |
9 |
double tanh(double x) |
10 |
double exp(double x) |
11 |
double frexp(double x, int *exponent) |
12 |
double ldexp(double x, int exponent) |
13 |
double log(double x) |
14 |
double log10(double x) |
15 |
double modf(double x, double *integer) |
16 |
double pow(double x, double y) |
17 |
double sqrt(double x) |
18 |
double ceil(double x) |
19 |
double fabs(double x) |
20 |
double floor(double x) |
21 |
double fmod(double x, double y) |
9.3.1 acos()
Description: The C library function double acos(double x) returns the arc cosine of x in radians.
Declaration: Following is the declaration for acos() function.
Syntax: double acos(double x)
Parameters: x -- This is the floating point value in the interval [-1,+1].
Return Value: This function returns principal arc cosine of x, in the interval [0, pi] radians.
The following example shows the usage of acos() function.
#include <stdio.h>
#include <math.h>
#define PI 3.14159265
int main ()
{
double x, ret, val;
x = 0.9;
val = 180.0 / PI;
ret = acos(x) * val;
printf("The arc cosine of %lf is %lf degrees", x, ret);
return(0);
}
Output:
The arc cosine of 0.900000 is 25.855040 degrees
9.3.2 asin()
Description: The C library function double asin(double x) returns the arc sine of x in radians.
Declaration: Following is the declaration for asin() function.
Syntax: double asin(double x)
Parameters: x -- This is the floating point value in the interval [-1,+1].
Return Value: This function returns the arc sine of x, in the interval [-pi/2,+pi/2] radians.
The following example shows the usage of asin() function.
#include <stdio.h>
#include <math.h>
#define PI 3.14159265
int main ()
{
double x, ret, val;
x = 0.9;
val = 180.0 / PI;
ret = asin(x) * val;
printf("The arc sine of %lf is %lf degrees", x, ret);
return(0);
}
Let us compile and run the above program, this will produce the following result:
The arc sine of 0.900000 is 64.190609 degrees
9.3.3 atan()
Description: The C library function double atan(double x) returns the arc tangent of x in radians.
Declaration: Following is the declaration for atan() function.
Syntax: double atan(double x)
Parameters: x -- This is the floating point value.
Return Value: This function returns the principal arc tangent of x, in the interval [-pi/2,+pi/2] radians.
The following example shows the usage of atan() function.
#include <stdio.h>
#include <math.h>
#define PI 3.14159265
int main ()
{
double x, ret, val;
x = 1.0;
val = 180.0 / PI;
ret = atan (x) * val;
printf("The arc tangent of %lf is %lf degrees", x, ret);
return(0);
}
Output:
The arc tangent of 1.000000 is 45.000000 degrees
9.3.4 exp()
Description: The C library function double exp(double x) returns the value of e raised to the xth power.
Declaration: Following is the declaration for exp() function.
Syntax: double exp(double x)
Parameters: x -- This is the floating point value.
Return Value: This function returns the exponential value of x.
The following example shows the usage of exp() function.
#include <stdio.h>
#include <math.h>
int main ()
{
double x = 0;
printf("The exponential value of %lf is %lf\n", x, exp(x));
printf("The exponential value of %lf is %lf\n", x+1, exp(x+1));
printf("The exponential value of %lf is %lf\n", x+2, exp(x+2));
return(0);
}
Output:
The exponential value of 0.000000 is 1.000000
The exponential value of 1.000000 is 2.718282
The exponential value of 2.000000 is 7.389056
9.3.5 log()
Description: The C library function double log(double x) returns the natural logarithm (base-e logarithm) of x.
Declaration: Following is the declaration for log() function.
Syntax: double log(double x)
Parameters: x -- This is the floating point value.
Return Value: This function returns natural logarithm of x.
The following example shows the usage of log() function.
#include <stdio.h>
#include <math.h>
int main ()
{
double x, ret;
x = 2.7;
/* finding log(2.7) */
ret = log(x);
printf("log(%lf) = %lf", x, ret);
return(0);
}
Output:
log(2.700000) = 0.993252
9.3.6 log10()
Declaration: Following is the declaration for log10() function.
Syntax: double log10(double x)
Parameters: x -- This is the floating point value.
Return Value: This function returns the common logarithm of x, for values of x greater than zero.
The following example shows the usage of log10() function.
#include <stdio.h>
#include <math.h>
int main ()
{
double x, ret;
x = 10000;
/* finding value of log
10
10000 */
ret = log10(x);
printf("log10(%lf) = %lf\n", x, ret);
return(0);
}
Output:
log10(10000.000000) = 4.000000
9.3.7 pow()
Description: The C library function double pow(double x, double y) returns x raised to the power of y i.e. xy.
Declaration: Following is the declaration for pow() function.
double pow(double x, double y)
Parameters: x -- This is the floating point base value.
y -- This is the floating point power value.
Return Value: This function returns the result of raising x to the power y.
The following example shows the usage of pow() function.
#include <stdio.h>
#include <math.h>
int main ()
{
printf("Value 8.0 ^ 3 = %lf\n", pow(8.0, 3));
printf("Value 3.05 ^ 1.98 = %lf", pow(3.05, 1.98));
return(0);
}
Output:
Value 8.0 ^ 3 = 512.000000
Value 3.05 ^ 1.98 = 9.097324
9.3.8 sqrt()
Description: The C library function double sqrt(double x) returns the square root of x.
Declaration: Following is the declaration for sqrt() function.
Syntax: double sqrt(double x)
Parameters: x -- This is the floating point value.
Return Value: This function returns the square root of x.
The following example shows the usage of sqrt() function.
#include <stdio.h>
#include <math.h>
int main ()
{
printf("Square root of %lf is %lf\n", 4.0, sqrt(4.0) );
printf("Square root of %lf is %lf\n", 5.0, sqrt(5.0) );
return(0);
}
Output:
Square root of 4.000000 is 2.000000
Square root of 5.000000 is 2.236068
9.3.9 ceil()
Description: The C library function double ceil(double x) returns the smallest integer value greater than or equal to x.
Declaration: Following is the declaration for ceil() function.
Syntax: double ceil(double x)
Parameters: x -- This is the floating point value.
Return Value: This function returns the smallest integral value not less than x.
The following example shows the usage of ceil() function.
#include <stdio.h>
#include <math.h>
int main ()
{
float val1, val2, val3, val4;
val1 = 1.6;
val2 = 1.2;
val3 = 2.8;
val4 = 2.3;
printf ("value1 = %.1lf\n", ceil(val1));
printf ("value2 = %.1lf\n", ceil(val2));
printf ("value3 = %.1lf\n", ceil(val3));
printf ("value4 = %.1lf\n", ceil(val4));
return(0);
}
Output:
value1 = 2.0
value2 = 2.0
value3 = 3.0
value4 = 3.0
9.3.10 fabs()
Description: The C library function double fabs(double x) returns absolute value of x.
Declaration: Following is the declaration for fabs() function.
Syntax: double fabs(double x)
Parameters: x -- This is the floating point value.
Return Value: This function returns the absolute value of x.
The following example shows the usage of fabs() function.
#include <stdio.h>
#include <math.h>
int main ()
{
int a, b;
a = 1234;
b = -344;
printf("The absolute value of %d is %lf\n", a, fabs(a));
printf("The absolute value of %d is %lf\n", b, fabs(b));
return(0);
}
Output:
The absolute value of 1234 is 1234.000000
The absolute value of -344 is 344.000000
9.3.11 floor()
Description: The C library function double floor(double x) returns the largest integer value less than or equal to x.
Declaration: Following is the declaration for floor() function.
Syntax: double floor(double x)
Parameters: x -- This is the floating point value.
Return Value: This function returns the largest integral value not greater than x.
The following example shows the usage of floor() function.
#include <stdio.h>
#include <math.h>
int main ()
{
float val1, val2, val3, val4;
val1 = 1.6;
val2 = 1.2;
val3 = 2.8;
val4 = 2.3;
printf("Value1 = %.1lf\n", floor(val1));
printf("Value2 = %.1lf\n", floor(val2));
printf("Value3 = %.1lf\n", floor(val3));
printf("Value4 = %.1lf\n", floor(val4));
return(0);
}
Output:
Value1 = 1.0
Value2 = 1.0
Value3 = 2.0
Value4 = 2.0
9.3.12 fmod()
Description: The C library function double fmod(double x, double y) returns the remainder of x divided by y.
Declaration: Following is the declaration for fmod() function.
Syntax: double fmod(double x, double y)
Parameters: x -- This is the floating point value with the division numerator i.e. x.
y -- This is the floating point value with the division denominator i.e. y.
Return Value: This function returns the remainder of dividing x/y.
The following example shows the usage of fmod() function.
#include <stdio.h>
#include <math.h>
int main ()
{
float a, b;
int c;
a = 9.2;
b = 3.7;
c = 2;
printf("Remainder of %f / %d is %lf\n", a, c, fmod(a,c));
printf("Remainder of %f / %f is %lf\n", a, b, fmod(a,b));
return(0);
}
Output:
Remainder of 9.200000 / 2 is 1.200000
Remainder of 9.200000 / 3.700000 is 1.800000
9.4 String manipulation <string.h>
The string .h header defines one variable type, one macro and various functions for manipulating arrays of characters.
9.4.1 Following is the variable type defined in the header string.h:
S.N. |
Variable & Description |
1 |
size_t |
9.4.2 Library Macros
Following is the macro defined in the header string.h:
S.N. |
Macro & Description |
1 |
NULL |
9.4.3 Following are the functions defined in the header string.h:
S.N. |
Function & Description |
1 |
void *memchr(const void *str, int c, size_t n) |
2 |
int memcmp(const void *str1, const void *str2, size_t n) |
3 |
void *memcpy(void *dest, const void *src, size_t n) |
4 |
void *memmove(void *dest, const void *src, size_t n) |
5 |
void *memset(void *str, int c, size_t n) |
6 |
char *strcat(char *dest, const char *src) |
7 |
char *strncat(char *dest, const char *src, size_t n) |
8 |
char *strchr(const char *str, int c) |
9 |
int strcmp(const char *str1, const char *str2) |
10 |
int strncmp(const char *str1, const char *str2, size_t n) |
11 |
int strcoll(const char *str1, const char *str2) |
12 |
char *strcpy(char *dest, const char *src) |
13 |
char *strncpy(char *dest, const char *src, size_t n) |
14 |
size_t strcspn(const char *str1, const char *str2) |
15 |
char *strerror(int errnum) |
16 |
size_t strlen(const char *str) |
17 |
char *strpbrk(const char *str1, const char *str2) |
18 |
char *strrchr(const char *str, int c) |
19 |
size_t strspn(const char *str1, const char *str2) |
20 |
char *strstr(const char *haystack, const char *needle) |
21 |
char *strtok(char *str, const char *delim) |
22 |
size_t strxfrm(char *dest, const char *src, size_t n) |
9.4.4 strcat()
Description: The C library function char *strcat(char *dest, const char *src) appends the string pointed to by src to the end of the string pointed to by dest.
Declaration: Following is the declaration for strcat() function.
Syntax: char *strcat(char *dest, const char *src)
Parameters: dest -- This is pointer to the destination array, which should contain a C string, and be large enough to contain the concatenated resulting string.
src -- This is the string to be appended. This should not overlap destination.
Return Value: This function return a pointer to the resulting string dest.
The following example shows the usage of strcat() function.
#include <stdio.h>
#include <string.h>
int main ()
{
char src[50], dest[50];
strcpy(src, "This is source");
strcpy(dest, "This is destination");
strcat(dest, src);
printf("Final destination string : |%s|", dest);
return(0);
}
Output:
Final destination string : |This is destinationThis is source|
9.4.5 strncat()
-
Description: The C library function char *strncat(char *dest, const char *src, size_ t n) appends the string pointed to by src to the end of the string pointed to by dest up to n characters long.
-
Declaration: Following is the declaration for strncat() function.
-
Syntax: char *strncat(char *dest, const char *src, size_t n)
-
Parameters: dest -- This is pointer to the destination array, which should contain a C string, and be large enough to contain the concatenated resulting string which includes the additional null-character.
src -- This is the string to be appended.
n -- This is the maximum number of characters to be appended.
-
Return Value: This function return a pointer to the resulting string dest.
The following example shows the usage of strncat() function.
#include <stdio.h>
#include <string.h>
int main ()
{
char src[50], dest[50];
strcpy(src, "This is source");
strcpy(dest, "This is destination");
strncat(dest, src, 15);
printf("Final destination string : |%s|", dest);
return(0);
}
Output:
Final destination string : |This is destinationThis is source|
9.4.6 strchr()
Description: The C library function char *strchr(const char *str, int c) searches for the first occurrence of the character c (an unsigned char) in the string pointed to by the argument str.
Declaration: Following is the declaration for strchr() function.
Syntax: char *strchr(const char *str, int c)
Parameters: str -- This is the C string to be scanned.
c -- This is the character to be searched in str.
Return Value: This returns a pointer to the first occurrence of the character c in the string str, or NULL if the character is not found.
The following example shows the usage of strchr() function.
#include <stdio.h>
#include <string.h>
int main ()
{
const char str[] = "http://www.tutorialspoint.com";
const char ch = '.';
char *ret;
ret = strchr(str, ch);
printf("String after |%c| is - |%s|\n", ch, ret);
return(0);
}
Output:
String after |.| is - |.tutorialspoint.com|
9.4.7 strcmp()
Description: The C library function int strcmp(const char *str1, const char *str2) compares the string pointed to by str1 to the string pointed to by str2.
Declaration: Following is the declaration for strcmp() function.
Syntax: int strcmp(const char *str1, const char *str2)
Parameters: str1 -- This is the first string to be compared.
str2 -- This is the second string to be compared.
Return Value: This function returned values are as follows:
if Return value if < 0 then it indicates str1 is less than str2
if Return value if > 0 then it indicates str2 is less than str1
if Return value if = 0 then it indicates str1 is equal to str2
The following example shows the usage of strncmp() function.
#include <stdio.h>
#include <string.h>
int main ()
{
char str1[15];
char str2[15];
int ret;
strcpy(str1, "abcdef");
strcpy(str2, "ABCDEF");
ret = strcmp(str1, str2);
if(ret > 0)
{
printf("str1 is less than str2");
}
else if(ret < 0)
{
printf("str2 is less than str1");
}
else
{
printf("str1 is equal to str2");
}
return(0);
}
Output:
str1 is less than str2
9.4.8 strcpy()
Description: The C library function char *strcpy(char *dest, const char *src) copies the string pointed to by src to dest.
Declaration: Following is the declaration for strcpy() function.
Syntax: char *strcpy(char *dest, const char *src)
Parameters:
dest -- This is the pointer to the destination array where the content is to be copied.
src -- This is the string to be copied.
Return Value: This return a pointer to the destination string dest.
Output:
#include <stdio.h>
#include <string.h>
int main()
{
char src[40];
char dest[12];
memset(dest, '\0', sizeof(dest));
strcpy(src, "This is tutorialspoint.com");
strcpy(dest, src);
printf("Final copied string : %s\n", dest);
return(0);
}
Output:
Final copied string : This is tutorialspoint.com
9.4.9 strcspn()
Description: The C library function size_t strcspn(const char *str1, const char *str2) calculates the length of the initial segment of str1 which consists entirely of characters not in str2.
Declaration: Following is the declaration for strcspn() function.
Syntax: size_t strcspn(const char *str1, const char *str2)
Parameters:
str1 -- This is the main C string to be scanned.
str2 -- This is the string containing a list of characters to match in str1.
Return Value: This function returns the number of characters in the initial segment of string str1 which are not in the string str2.
The following example shows the usage of strcspn() function.
#include <stdio.h>
#include <string.h>
int main ()
{
int len;
const char str1[] = "ABCDEF4960910";
const char str2[] = "013";
len = strcspn(str1, str2);
printf("First matched character is at %d\n", len + 1);
return(0);
}
Output:
First matched character is at 10
9.4.10 strlen()
Description: The C library function size_t strlen(const char *str) computes the length of the string str up to but not including the terminating null character.
Declaration: Following is the declaration for strlen() function.
Syntax: size_t strlen(const char *str)
Parameters: str -- This is the string whose length is to be found.
Return Value: This function returns the length of string.
The following example shows the usage of strlen() function.
#include <stdio.h>
#include <string.h>
int main ()
{
char str[50];
int len;
strcpy(str, "This is tutorialspoint.com");
len = strlen(str);
printf("Length of |%s| is |%d|\n", str, len);
return(0);
}
Output:
Length of |This is tutorialspoint.com| is |26|
9.4.11 strrchr()
Description: The C library function char *strrchr(const char *str, int c) searches for the last occurrence of the character c (an unsigned char) in the string pointed to by the argument str.
Declaration: Following is the declaration for strrchr() function.
Syntax: char *strrchr(const char *str, int c)
Parameters:
str -- This is the C string.
c -- This is the character to be located. It is passed as its int promotion, but it is internally converted back to char.
Return Value: This function returns a pointer to the last occurrence of character in str. If the value is not found, the function returns a null pointer.
The following example shows the usage of strrchr() function.
#include <stdio.h>
#include <string.h>
int main ()
{
int len;
const char str[] = "http://www.tutorialspoint.com";
const char ch = '.';
char *ret;
ret = strrchr(str, ch);
printf("String after |%c| is - |%s|\n", ch, ret);
return(0);
}
Output:
String after |.| is - |.com|
9.4.12 strstr()
Description: The C library function char *strstr(const char *haystack, const char *needle) function finds the first occurrence of the substring needle in the string haystack. The terminating '\0' characters are not compared.
Declaration: Following is the declaration for strstr() function.
Syntax: char *strstr(const char *haystack, const char *needle)
Parameters:
haystack -- This is the main C string to be scanned.
needle -- This is the small string to be searched with-in haystack string.
Return Value: This function returns a pointer to the first occurrence in haystack of any of the entire sequence of characters specified in needle, or a null pointer if the sequence is not present in haystack.
The following example shows the usage of strstr() function.
#include <stdio.h>
#include <string.h>
int main()
{
const char haystack[20] = "TutorialsPoint";
const char needle[10] = "Point";
char *ret;
ret = strstr(haystack, needle);
printf("The substring is: %s\n", ret);
return(0);
}
Output:
The substring is: Point
http://www.tutorialspoint.com/c_standard_library/c_function_isalnum.htm