STRING FUNCTIONS
// Using strcpy and strncpy
#include <stdio.h>
#include <string.h>
main()
{
char x[] = "Happy Birthday to
You";
char y[25], z[15];
printf("The string in array x is: %s\n",x);
printf("The string in array y is: %s\n",strcpy(y, x));
strncpy(z, x, 14);
z[14] = '\0';
printf("The string in array z is: %s\n", z);
return 0;
}
/*
The string in array x is: Happy Birthday to You
The string in array y is: Happy Birthday to You
The string in array z is: Happy Birthday
*/
// Using strcat and strncat
#include <stdio.h>
#include <string.h>
main()
{
char s1[20] = "Happy ";
char s2[] = "New Year ";
char s3[40] = "";
printf("s1 = %s\ns2 = %s\n",s1,s2);
printf("strcat(s1, s2) =
%s\n",strcat(s1, s2));
printf("strncat(s3, s1,
6) = %s\n",strncat(s3, s1, 6));
printf("strcat(s3, s1) =
%s\n",strcat(s3, s1));
return 0;
}
/*
s1 = Happy
s2 = New Year
strcat(s1, s2) = Happy New Year
strncat(s3, s1, 6) = Happy
strcat(s3, s1) = Happy Happy New Year
*/
// Using strcmp and strncmp
#include <string.h>
#include <stdio.h>
main()
{
char *s1 = "Happy New Year";
char *s2 = "Happy New Year";
char *s3 = "Happy Holidays";
printf("\ns1 = %s\ns2 = %s\ns3 = ",s1,s2,s3);
printf("strcmp(s1, s2) = %d\n",strcmp(s1, s2));
printf("strcmp(s1, s3) = %d\n",strcmp(s1, s3));
printf("strcmp(s3, s1) = %d\n\n",strcmp(s3, s1));
printf("strncmp(s1, s3, 6) = %d\n",strncmp(s1, s3, 6));
printf("strncmp(s1, s3, 6) = %d\n",strncmp(s1, s3, 7));
printf("strncmp(s3, s1, 7) = %d\n",strncmp(s3, s1, 7));
return 0;
}
/*
s1 = Happy New Year
s2 = Happy New Year
s3 = strcmp(s1, s2) = 0
strcmp(s1, s3) = 1
strcmp(s3, s1) = -1
strncmp(s1, s3, 6) = 0
strncmp(s1, s3, 6) = 1
strncmp(s3, s1, 7) = -1
*/
// Using strtok
#include <iostream.h>
#include <string.h>
/*Multiple calls to strtok are required to break a string into tokens.
The first call to strtok contains two
arguments, a string to be
tokenized, and a string containing
characters that separate the tokens.
tokenPtr = strtok(string,
" ")
assigns tokenPtr a pointer to the first
token in string. The second
argument of strtok,
" ", indicates that tokens in string are separated
by spaces. Function strtok searches for the first
character in string
that is not a delimiting character
(space). This begins the first
token.
The function then finds the next
delimiting character in the string and r
replaces it wiht the null ('\0')
character. This terminates the current
token.
Function strtok saves a pointer to the next characterr following
the token in string, and returns a
pointer to the current token.
Subsequent calls to strtok to continue
tokenizing string contain NULL as
the first argument. The NULL argument indicates that the call to
strtok
should continue tokenizing from the
location in string saved by the last
call to strtok. If no tokens remain when strtok is called,
strtok return
NULL.
*/
main()
{
char string[] = "This is a sentence with 7 tokens";
char *tokenPtr;
printf("The string to be tokenized is:\n\n%s\n",string);
printf("\n\nThe tokens are:\n");
tokenPtr = strtok(string, " ");
while (tokenPtr != NULL)
{
printf("%s\n",tokenPtr);
tokenPtr = strtok(NULL, " ");
}
return 0;
}
/*
The string to be tokenized is:
This is a sentence with 7 tokens
The tokens are:
This
is
a
sentence
with
7
tokens
*/
// Using strlen
#include <stdio.h>
#include <string.h>
main()
{
char *string1 = "abcdefghijklmnopqrstuvwxyz";
char *string2 = "four";
char *string3 = "Boston";
printf("The length of \"%s\" is %d\n",string1,strlen(string1));
printf("The length of \"%s\" is
%d\n",string2,strlen(string2));
printf("The length of \"%s\" is
%d\n",string3,strlen(string3));
return 0;
}
/*
The length of
"abcdefghijklmnopqrstuvwxyz" is 26
The length of "four" is 4
The length of "Boston" is
6
*/
//String Conversion Functions
#include <iostream.h>
#include <stdlib.h> //General
utilities library
//nPtr is a pointer to a character constant.
//const declares that the argument value will not be modified.
// Function prototype Function Description
//double atof(const char *nPtr)
Converts the string nPtr to double.
//int atof(const char *nPtr)
Converts the string nPtr to int.
//long atof(const char *nPtr)
Converts the string nPtr to long int.
//double strtod(const char *nPtr, char **endPtr)
//
Converts the string nPtr to double.
//long strtol(const char *nPtr, char **endPtr, int base)
//
Converts the string nPtr to long.
//
base is number base, octal, decimal etc.
//unsigned long strtoul(const char *nPtr, char **endPtr, int base)
//
Converts the string nPtr to unsigned long..
main()
{
// Using atof
double d = atof("99.0");
printf("The string
\"99.0\" converted to double is %g\n",d);
printf("The converted value
divided by 2 is %g\n", d/2.0);
printf("\n");
/*
The string "99.0" converted to double is 99
The converted value divided by 2 is 49.5
*/
// Using atoi
int i = atoi("2593");
printf("The string
\"2593\" converted to int is %d\n",i);
printf("The converted value minus
593 is %d\n", i - 593);
printf("\n");
/*
The string "2593" converted to int is 2593
The converted value minus 593 is 2000
*/
// Using atol
long l = atol("1000000");
printf("The string
\"1000000\" converted to long is %lu\n",l);
printf("The converted value
divided by 2 is %lu\n", l/2);
printf("\n");
/*
The string "1000000" converted to long is 1000000
The converted value divided by 2 is 500000
*/
// Using strtod
char *string = "51.2% are
admitted", *stringPtr;
d = strtod(string, &stringPtr);
printf("The string
\"%s\" is converted to the\n",string);
printf("double value %g and the
string \"%s\"\n",d,stringPtr);
printf("\n");
/*
The string "51.2% are admitted" is converted to the
double value 51.2 and the string "% are admitted"
*/
// Using strtol
long x;
char *remainderPtr;
string = "-1234567abc";
x = strtol(string, &remainderPtr,
0);
printf("The original string is
\"%s\"\n",string);
printf("The converted value is
%d\n",x);
printf("The remainder of the
original string is \"%s\"\n",
remainderPtr);
printf("The converted value plus
567 is %d\n",x+567);
printf("\n");
/*
The original string is "-1234567abc"
The converted value is -1234567
The remainder of the original string is "abc"
The converted value plus 567 is -1234000
*/
// Using strtoul
unsigned long xx;
string = "1234567abc";
xx = strtoul(string,
&remainderPtr, 0);
printf("The original string is
\"%s\"\n",string);
printf("The converted value is
%lu\n",xx);
printf("The remainder of the
original string is \"%s\"\n",
remainderPtr);
printf("The converted value minus
567 is %lu\n",xx-567);
printf("\n");
/*
The original string is "1234567abc"
The converted value is 1234567
The remainder of the original string is "abc"
The converted value minus 567 is 1234000
*/
return 0;
}
// Using strchr
#include <stdio.h>
#include <string.h>
/* char *strchr(const *s, int c);
Locates the first occurrence of character c in string s. if
c is found, a pointer to c in s is returned.
Otherwise, a
NULL pointer is returned.
*/
main()
{
char *string = "This is a
test";
char character1 = 'a', character2 =
'z';
if (strchr(string, character1) !=
NULL)
printf("\'%c\' was found in
\"%s\".\n",character1,string);
else
printf("\'%c\' was not found
in \"%s\".\n",character1,string);
if (strchr(string, character2) !=
NULL)
printf("\'%c\' was found in
\"%s\".\n",character2,string);
else
printf("\'%c\' was not found
in \"%s\".\n",character2,string);
return 0;
}
/* Output
'a' was found in "This is a test".
'z' was not found in "This is a test".
*/
// Using strcspn
#include <stdio.h>
#include <string.h>
/* size_t strcspn(const char *s1, const char *s2);
Determines and returns the length of
the initial segment
of string s1 consisting of characters
not contained in s2.
*/
main()
{
char *string1 = "The value is
3.14159";
char *string2 = "1234567890";
printf("string1 =
%s\n",string1);
printf("string2 =
%s\n\n",string2);
printf("The length of the initial
segment of string1\n");
printf("containing no characters
from string2 = %d\n",
strcspn(string1,
string2));
return 0;
}
/*
string1 = The value is 3.14159
string2 = 1234567890
The length of the initial segment of string1
containing no characters from string2 = 13
*/
#include <stdio.h>
#include <string.h>
/* char *strpbrk(const char *s1, const char *s2);
Locates the first occurrence in string
s1 of any character
in string s2. If a character from string s2 is found, a
pointer
to the character in string s1 is
returned. Otherwise a NULL
pointer is returned.
*/
main()
{
char *string1 = "This is a
test";
char *string2 = "beware";
printf("Of the characters in
\"%s\"\n",string2);
printf("'%c' is the first
character to appear in\n%s\n",
*strpbrk(string1,string2),string1);
return 0;
}
/*
Of the characters in "beware"
'a' is the first character to appear in
"This is a test"
*/
// Using strrchr
#include <stdio.h>
#include <string.h>
/* char *strrchr(const *s, int c);
Locates the last occurrence of c in
string s. If c is found,
a pointer to c in string s is returned. Otherwise, a NULL
pointer is returned.
*/
main()
{
char *string1 = "A zoo has many
animals including zebras";
int c = 'z';
printf("The remainder of string1
beginning with the\n");
printf("last occurrence of
character '%c' is: %s\n",
c , strrchr(string1, c));
return 0;
}
/*
The remainder of string1 beginning with the
last occurrence of character 'z' is: "zebras"
*/
// Using strspn
#include <stdio.h>
#include <string.h>
/* size_t strspn(const char *s1, const
char *s2);
Determines and returns the length of
the initial segment of
string s1 consisting only of
characters contained in string s2.
*/
main()
{
char *string1 = "The value is
3.14159";
char *string2 = "aehilsTuv
";
printf("string1 = %s\n",string1);
printf("string2 =
%s\n\n",string2);
printf("The length of the initial
segment of string1\n");
printf("containing only
characters from string2 = %d\n",
strspn(string1,
string2));
return 0;
}
/*
string1 = The value is 3.14159
string2 = aehilsTuv
The length of the initial segment of string1
containing only characters from string2 = 13
*/
// Using strstr
#include <stdio.h>
#include <string.h>
/* char *strstr(const char *s1, const char *s2);
Locates the first occurrence in string
s1 of string s2. If
the string is found, a pointer to the
string in s1 is
returned. Otherwise, a NULL pointer is returned.
*/
main()
{
char *string1 =
"abcdefabcdef";
char *string2 = "def";
printf("string1 =
%s\n",string1);
printf("string2 =
%s\n\n",string2);
printf("The remainder of string1
beginning with the\n");
printf("first occurrence of
string2 is: %s\n",
strstr(string1,
string2));
return 0;
}
/*
string1 = abcdefabcdef
string2 = def
The remainder of string1 beginning with the
first occurrence of string2 is: defabcdef
*/
// Using memcpy
#include <stdio.h>
#include <string.h>
/* void *memcpy(void *s1, const void
*s2, size_t n);
Copies n characters from the object
pointed to by s2 into the
object pointed to by s1. A pointer to the resulting object is
returned.
*/
main()
{
char s1[17], s2[] = "Copy this string";
memcpy(s1, s2, 17);
printf("After s2 is copied into
s1 with memcpy,\n");
printf("s1 contains
\"%s\"\n",s1);
return 0;
}
/*
After s2 is copied into s1 with memcpy,
s1 contains "Copy this string"
*/
// Using memmove
#include <stdio.h>
#include <string.h>
/* void *memmove(void *s1, const void *s2, size_t n);
Copies n characters from the object
pointed to by s2 into
the object pointed to by s1. The copy is performed as if
the characters are first copied from
the object pointed to
by s2 into a temporary array, then
from the temporary array
into the object pointed to by s1. A pointer to the
resulting object is returned.
*/
main()
{
char x[] = "Home Sweet
Home";
printf("The string in array x
before memmove is: %s\n",x);
printf("The string in array x
after memmove is: %s\n",
(char *) memmove(x, &x[5],
10));
return 0;
}
/*
The string in array x before memmove is: Home Sweet Home
The string in array x after memmove is:
Sweet Home Home
*/
// Using memcmp
#include <stdio.h>
#include <string.h>
/* int memcmp(const void *s1, const void *s2, size_t n);
Compares the first n characters of the
object pointed to by
s1 and s2. The function returns 0, less than 0, or
greater
than 0 if s1 is equalto, less than, or
greater than s2.
*/
main()
{
char s1[] = "ABCDEFG", s2[]
= "ABCDXYZ";
printf("s1 = %s\n",s1);
printf("s2 = %s\n\n",s2);
printf("memcmp(s1, s2, 4) =
%d\n",memcmp(s1, s2, 4));
printf("memcmp(s1, s2, 7) =
%d\n",memcmp(s1, s2, 7));
printf("memcmp(s2, s1, 7) =
%d\n",memcmp(s2, s1, 7));
return 0;
}
/*
s1 = ABCDEFG
s2 = ABCDXYZ
memcmp(s1, s2, 4) = 0
memcmp(s1, s2, 7) = -224
memcmp(s2, s1, 7) = 32
*/
// Using memchr
#include <stdio.h>
#include <string.h>
/* void *memchr(const void *s, int
c, size_t n);
Locates the first occurrence of c
(converted to unsigned char)
in the first n characters of the
object pointed to by s. If c is
found, a pointer ot c in the object is
returned.
Otherwise, NULL is returned.
*/
main()
{
char *s = "This is a
string";
printf("The remainder of s after
character 'r' is found is \"%s\"\n",
(char *) memchr(s, 'r', 16));
// cout << "The remainder of
s after character 'r' is found is \""
// << (char *) memchr(s, 'r',
16) << '\"' << endl;
return 0;
}
/*
The remainder of s after character 'r' is found is "ring"
*/
// Using memset
#include <stdio.h>
#include <string.h>
/* void *memset(void *s, int c,
size_t n);
Copies c (converted to unsigned char)
into the first
n characters of the object pointed to
by s.
A pointer to the result is returned.
*/
main()
{
char string1[15] =
"BBBBBBBBBBBBBB";
printf("string1 =
%s\n",string1);
printf("string1 after memset =
%s\n",(char *) memset(string1, 'b', 7));
return 0;
}
/*
string1 = BBBBBBBBBBBBBB
string1 after memset = bbbbbbbBBBBBBB
*/