In this section, we will discuss some character and string manipulation functions that are part of ctype.h, string.h, and stdlib.h.
MISCELLANEOUS
STRING AND CHARACTER FUNCTIONS
In
this section, we will discuss some character and string manipulation functions
that are part of ctype.h, string.h, and
stdlib.h.
Table
6.2 illustrates some character functions contained in ctype.h.
In
this section we will look at some commonly used string functions present in the
string.h header file.
strcat Function
Syntax:
char *strcat (char *strl, const
char *str2);
Programming Tip:
Before
using string copy and concatenating functions, ensure that the destination
string has enough space to store all the elements so that memory overwriting
does not take place.
The
strcat function appends the string pointed to by str2 to the end of the string
pointed to by strl. The terminating
null character of strl is
overwritten. The process stops when the terminating null character of str2 is copied. The argument strl is
returned. Note that strl should be big enough to store the contents of str2.
#include <stdio.h>
#include <string.h>
int main()
}
char str1 [50] =
"Programming";
char str2 [ ] = "In C";
strcat (str1, str2);
printf("\n str1: %s",
str1);
return 0;
}
Output
Str1: Programming In C
strncat Function
Syntax:
char *strncat (char *strl, const
char *str2, size_t n);
This
function appends the string pointed to by str2
to the end of the string pointed to by str1
up to n characters long. The
terminating null character of strl is
overwritten. Copying stops when n characters are copied or the terminating null
character of str2 is copied. A
terminating null character is appended to str1
before returning to the calling function. and
#include <stdio.h>
#include <string.h>
int main()
{
char str1 [50] =
"Programming";
char str2 [] = "In C";
strncat (strl, str2, 2);
printf("\n str1: %s",
str1);
return 0;
}
Output
strl: Programming In
strchr Function
Syntax:
char *strchr(const char *str, int
c);
The
strchr() function searches for the
first occurrence of the character c (an unsigned char) in the string pointed to
by the argument str. The function
returns a pointer pointing to the first matching character, or null if no match
is found.
#include <stdio.h>
#include <string.h>
int main()
{
char str[50] = "Programming In
C";
char *pos;
pos = strchr(str, 'n');
if (pos)
printf("\n n is found in str
at position %d", pos);
else
printf("\n n is not present in
the string");
return 0;
}
Output
n is found in str at position 9
strrchr Function
Syntax:
char *strrchr (const char *str, int
c);
The
strrchr() function searches for the
first occurrence of the character c (an unsigned char) beginning at the rear
end and working towards the front in the string pointed to by the argument str, i.e., the function searches for the
last occurrence of the character c and returns a pointer pointing to the last
matching character, or null if no match is found.
#include <stdio.h>
#include <string.h>
int main()
{
char str[50] = "Programming In
C";
char *pos;
pos = strrchr(str, 'n');
if (pos)
printf("\n The last position
of n is: %d", pos-str);
else
printf("\n n is not present in
the string");
return 0;
}
Output
The last position of n is: 13
strcmp Function
int strcmp (const char *strl, const
char *str2);
The
strcmp function compares the string
pointed to by str1 to the string pointed to by str2. The function returns zero if the strings are equal.
Otherwise, it returns a value less than zero or greater than zero if str1 is
less than or greater than str2
respectively.
#include <stdio.h>
#include <string.h>
int main()
{
char str1 [10] = "HELLO";
char str2 [10] = "HEY";
if (strcmp(str1, str2)==0)
printf("\n The two strings are
identical");
else
printf("\n The two strings are
not identical");
return 0;
}
Output
The two strings are not identical
strncmp Function
Syntax:
int strncmp (const char *strl,
const char *str2, size_t n);
This
function compares at most the first n bytes of strl and str2. The
process stops comparing after the null character is encountered. The function
returns zero if the first n bytes of the strings are equal. Otherwise, it
returns a value less than zero or greater than zero if strl is less than or
greater than str2, respectively.
#include <stdio.h>
#include <string.h>
int main()
{
char str1 [10] = "HELLO";
char str2 [10] = "HEY";
if (strncmp (strl, str2,2)==0)
printf("\n The two strings are
identical");
else
printf("\n The two strings are
not identical");
return 0;
}
Output
The two strings are identical
strcpy Function
Syntax:
char *strcpy (char *st rl, const
char *str2);
This
function copies the string pointed to by str2
str1 including the null character of str2.
It returns the argument strl. Here str1 should be big enough to store the
contents of str2.
#include <stdio.h>
#include <string.h>
int main()
{
char str1 [10], str2 [10] =
"HELLO";
strcpy(strl, str2);
printf("\n strl: %s",
strl);
return 0;
}
Output
HELLO
strncpy Function
Syntax:
char
*strncpy (char *str1, const char *str2, size t n);
Programming Tip:
Do
not use string functions on a character array that is not terminated with a
null character.
This
function copies up to n characters from the string to str1. pointed to by str2
to str1 Copying stops when n
characters are copied. However, if the null character in str2 is reached then the null character is continually copied to str1 until n characters have been copied.
Finally, a null character must be appended to str1. However, if n is zero or negative then nothing is copied.
#include <stdio.h>
#include <string.h>
int main()
{
char str1 [10], str2 [10] =
"HELLO";
strncpy (strl, str2, 2);
printf("\n str1: %s", str1);
return 0;
}
Output
HE
Note
To
copy the string str2 in str1, a better way is to write
strncpy(strl, str2, sizeof(strl)
−1);
This
would enforce the copying of only that much characters for which str1 has space to accommodate. We have
written size of str1 minus 1 to store
the null character.
strlen Function
Syntax:
Size_t strlen (const char *str);
This
function calculates the length of the string str up to but not including the null character, i.e., the function
returns the number of characters in the string.
#include <stdio.h>
#include <string.h>
int main()
{
char str[] = "HELLO";
printf("\n Length of str is:
%d", strlen(str));
return 0;
}
Output
Length of str is: 5
strstr Function
Syntax:
char *strstr(const char *strl,
const char *str2);
This
function is used to find the first occurrence of string str2 (not including the terminating null character) in the string str1. It returns a pointer to the first
occurrence of str2 in strl. If no match is found, then a null pointer is
returned.
#include <stdio.h>
#include <string.h>
int main()
{
char strl [] = "HAPPY BIRTHDAY
TO YOU";
char str2 [] = "DAY";
char *ptr;
ptr = strstr(str1, str2);
if (ptr)
printf("\n Substring
Found");
else
printf("\n Substring Not
Found");
return 0;
}
Output
Substring Found
strspn Function
Syntax:
size_t
strspn (const char *str1, const char *str2);
The
function returns the index of the first character in str1 that doesn't match any character in str2.
#include <stdio.h>
#include <string.h>
int main()
{
char str1 [] = "HAPPY BIRTHDAY
TO YOU";
char str2 [] = "HAPPY BIRTHDAY
JOE";
printf("\n The position of
first character in str2 that does not match with that in str1 is %d",
strspn (strl, str2));
return 0;
}
Output
The position of first character in
str2 that does not match with that in str1 is 15
strcspn Function
Syntax:
size_t strcspn (const char *strl,
const char *str2);
The
function returns the index of the first character in strl that matches any of
the characters in str2.
#include <stdio.h>
#include <string.h>
int main()
{
char str1 [] = "PROGRAMMING IN
C";
char str2 [] = "IN";
printf("\n The position of
first character in str2 that matches with that in str1 is %d", strcspn
(strl, str2));
return 0;
}
Output
The position of first character in
str2 that matches with that in str1 is 8
strpbrk Function
Syntax:
char *strpbrk (const char *str1,
const char *str2);
The
function strpbrk () returns a pointer
to the first occurrence in str1 of
any character in str2, or NULL if
none are present. The only difference between strpbrk() and strcspn is
that strcspn () returns the index of
the character and strpbrk () returns
a pointer to the first occurrence of a character in str2.
#include <stdio.h>
#include <string.h>
int main( )
{
char str1 [] = "PROGRAMMING IN
C";
char str2 [] = "AB";
char *ptr = strpbrk (strl, str2);
int main()
if (ptr = = NULL)
printf("\n No character
matches in the two strings");
else
printf("\n Character in str2
matches with that in str1");
return 0;
}
Output
No character matches in the two
strings
strtok Function
Syntax:
char
*strtok ( char *strl, const char *delimiter );
The
strtok () function is used to isolate sequential tokens in a null-terminated
string, str. These tokens are separated in the string using delimiters. The
first time that strtok is called, str should be specified; subsequent calls,
wishing to obtain further tokens from the same string, should pass a NULL
pointer instead. However, the delimiter must be supplied each time, though it
may change between calls.
The
strtok() function returns a pointer to the beginning of each subsequent token
in the string, after replacing the token itself with a NULL character. When all
tokens are left, a null pointer is returned.
#include <stdio.h>
#include <string.h>
main ()
{
char str[] = "Hello, to, the,
world of, programming";
char delim [] ", ";
char result [20];
result = strtok (str, delim);
while (result!= NULL)
{
printf("\n %s", result);
result = strtok (NULL, delim);
}
getch();
return 0;
}
Output
Hello
to
the
world of
programming
strtol Function
Syntax:
long strtol (const char *str, char
**end, int base);
The
strtol function converts the string
pointed by str to a long value. The function skips leading
white space characters and stops when it encounters the first non- numeric
character. strtol stores the address
of the first invalid character in str
in *end. If there were no digits at
all, then the strtol function will store the original value of str in *end. You may pass NULL
instead of *end if you do not want to
store the invalid characters anywhere. Finally, the third argument base
specifies whether the number is in hexa-decimal,
octal, binary, or decimal
representation.
#include <stdio.h>
#include <stdlib.h>
main ()
{
long num;
num = strtol ("12345 Decimal
Value", NULL, 10);
printf("%ld", num);
num = strtol ("65432 Octal
Value", NULL, 8);
printf("%ld", num);
strtol ("10110101 Binary
Value", NULL, 2);
printf("%ld", num);
num = strtol ("A7CB4
Hexadecimal Value",NULL, 16);
printf("%ld", num);
getch();
return 0;
}
Output
12345
27418
181
687284
strtod Function
Syntax:
double strtod (const char *str,
char **end);
The
function accepts a string str that
has an optional plus ('+') or minus sign (-) followed by either:
•
a decimal number containing a sequence of decimal digits optionally consisting
of a decimal point, or
•
a hexadecimal number consisting of a "0x" or "0x" followed
by a sequence of hexadecimal digits optionally containing a decimal point.
In
both cases, the number may be optionally followed by an exponent ('E' or 'e'
for decimal constants or a 'p' or 'p' for hexadecimal constants), followed by
an optional plus or minus sign, followed by a sequence of decimal digits. For
decimal constants and hexadecimal constants, the exponent indicates the power
of 10 and 2, respectively, by which the number should be scaled.
#include <stdio.h>
#include <stdlib.h>
main ()
{
double num;
num = strtod
("123.345abcdefg", NULL);
printf("%lf", num);
getch();
return 0;
}
Output
123.345000
atoi () Function
Till
now you must have understood that the value 1 is an integer and '1' is a
character. So there is a huge difference when we write the two statements given
below
int i=1; // here i =1
int i='1'; // here i =49, the ASCII
value of character 1
Similarly,
123 is an integer number but '123' is a string of digits. What if you want to
operate some integer operations on the string '123'? For this, C provides a
function atoi that converts a given string into its corresponding integer.
The
atoi() function converts a given
string passed to it as an argument into an integer. The atoi() function returns that integer to the calling function.
However, the string should start with a number. The atoi() will stop reading from the string as soon as it encounters a
non-numerical character. The atoi()
is included in the stdlib.h file. So
before using this function in your program, you must include this header file.
The syntax of atoi() can be given as,
int atoi( const char *str);
Example
i = atoi( "123.456" );
RESULT: i = 123
atof() Function
The
function atof () converts the string
that it accepts as an argument into a double value and then return that value
to the calling function. However, the string must start with a valid number.
One point to remember is that the string can be terminated with any
non-numerical character, other than "E" or "e". The syntax
of atof () can be given as,
double
atof ( const char *str);
Example
X = atof("12.39 is the
answer" );
RESULT: x = 12.39
atol() Functions
The
function atol () converts the string
into a long int value. The atol
function returns the converted long value to the calling function. Like atoi,
the atol () will read from a string
until it finds any character that should not be in a long. Its syntax can be
given as,
long atol ( const char *str);
Example
X = atol ( "12345.6789"
);
RESULT: x = 12345L.
Note
The
functions atoi(), atof(), and atol() are a part of stdlib.h header file.
Programming in C: Unit II (b): Strings : Tag: : Syntax with Example C Programs - Miscellaneous String and Character Functions
Programming in C
CS3251 2nd Semester CSE Dept 2021 | Regulation | 2nd Semester CSE Dept 2021 Regulation