Professional Documents
Culture Documents
CONTENTS
Introduction to Strings. Two Ways of using Strings. Initializing a string. Print a string. String Functions and its applications.
Introduction to Strings
A String is an array of characters. Strings are stored in an array of char type along with the null terminating character "\0" at the end. When sizing the string array we need to add plus one to the actual size of the string to make space for the null terminating character, "\0" But the null character is not included in the String. Character Strings are often used to build meaningful and Readable programs. The Common Operations performed on Character Strings include Reading and Writing Strings. Combined Strings together. Copying one String to another. Comparing Strings for Equality. Extracting a portion of a String.
Initializing a String
To initialize our fname string store the name Ravi, char fname[31] = {Ravi"}; We can observe from the above statement that initializing a string is same as with any array. However we also need to surround the string with quotes.
Print a String :
To write strings to the terminal, we use a file stream known as stdout. The most common function to use for writing to stdout in C is the printf function, defined as follows: int printf(const char *format, ...); To print out a prompt for the user we can: printf("Please type a name: \n"); The above statement prints the prompt in the quotes and moves the cursor to the next line.
Example for memcpy(): ptr = memcpy( s1, s2, N ); void *s1; points to an area of memory that is to receive the copied characters. This must be able to hold at least N characters. const void *s2; points to the string from which characters will be copied. size_t N; gives the number of characters to copy. void *ptr; points to the copied string (i.e. "s1").
EXAMPLE FOR MEMMOVE(): ptr = memmove( s1, s2, N ); void *s1; points to an area of memory that is to receive the moved characters. This must be able to hold at least N characters. const void *s2; points to the string from which characters will be copied. size_t N; gives the number of characters to copy. void *ptr; points to the copied string (i.e. "s1").
EXAMPLE FOR STRCPY(): ptr = strcpy( s1, s2 ); char *s1; points to an area of memory that is to receive the copied characters. const char *s2; points to the string from which characters will be copied. This must end with the usual '\0. char *ptr; points to the copied string (i.e. "s1").
EXAMPLE FOR STRNCPY(): ptr = strncpy( s1, s2, N ); char *s1; points to an area of memory that is to receive the copied characters. This must be able to hold at least N characters. const char *s2; points to the string from which characters will be copied. size_t N; gives the number of characters to copy. char *ptr; points to the copied string (i.e. "s1").
EXAMPLE FOR STRCAT(): ptr = strcat( s1, s2 ); char *s1; points to a string terminated by the usual '\0. const char *s2; points to a string that will be appended to "s1. char *ptr; points to the new string ("s1").
EXAMPLE FOR STRNCAT(): ptr = strncat( s1, s2, N ); char *s1;points to a string terminated by the usual '\0. const char *s2;points to a string whose characters will be appended to the end of "s1 size_t N;is the maximum number of characters from string "s2" that should be appended to "s1. char *ptr;points to the new string ("s1").
EXAMPLE FOR STRCMP(): i = strcmp( s1, s2 ); const char *s1, *s2; are the strings to be compared. int i; gives the results of the comparison. "i" is zero if the strings are identical. "i" is positive if string "s1" is greater than string "s2", and is negative if string "s2" is greater than string "s1". Comparisons of "greater than" and "less than" are made according to the ASCII collating sequence.
EXAMPLE FOR STRNCMP(): i = strncmp( s1, s2, N ); const char *s1, *s2; are the strings to be compared. size_t N; gives the number of characters to be examined. int i; gives the results of the comparison. "i" is zero if the first N characters of the strings are identical. "i" is positive if string "s1" is greater than string "s2", and is negative if string "s2" is greater than string "s1". Comparisons of "greater than" and "less than" are made according to the ASCII collating sequence.
EXAMPLE FOR MEMCHR(): ptr = memchr( s, c, N ); const void *s; points to the string to be scanned. int c; is the character to look for. size_t N; is the number of characters to look at before giving up. void *ptr; points to the first occurrence of the character in the string. If the character is not found, the NULL pointer is returned.
EXAMPLE FOR STRCHR(): ptr = strchr( s, c ); const char *s; points to the string that is to be scanned for the presence of the character. int c; is the character to look for. char *ptr; points to the first occurrence of the character in the string. If the character does not appear in the string, the NULL pointer is returned.
EXAMPLE FOR STRCSPN(): i = strcspn( s, stop ); const char *s; points to the string to be scanned. const char *stop; points to a string containing the set of "stopping" Characters. size_t i; gives the position of the first character from "stop that appears in "s".
EXAMPLE FOR STRPBRK(): ptr = strpbrk(string,set); const char *string; is the string you want to examine. const char *set; gives a set of characters to look for. char *ptr; points to the first character in "string" that also appears in "set". If no characters from "set" can be found in "string", "strpbrk" returns a null pointer.
EXAMPLE FOR STRRCHR(): ptr = strrchr( s, c ); const char *s; points to the string that is to be scanned for the presence of the character. int c; is the character to look for. char *ptr; points to the last occurrence of the character in the string. If the character does not appear in the string, a null pointer is returned.
EXAMPLE FOR STRSPN(). i = strspn( s, set ); const char *s; points to the string to be scanned. const char *set; points to a string containing the set of characters to scan for. size_t i; is the number of characters at the beginning of "s" which also appear in the string "set".
EXAMPLE FOR STRSTR(): ptr = strstr( s, subs ); const char *s; points to the string to be scanned. const char *subs; points to the (sub)string to scan for. char *ptr; points to the first occurrence of the substring in the given string. If the substring is not found, this will be a null pointer.
EXAMPLE FOR MEMSET(): ret = memset( p, value, N ); Where: void *p; points to the beginning of the memory that should be set to the given value. int value; is the value that is to be assigned to each byte. size_t N; is the number of bytes whose value is to be set. void *ret; points to the beginning of the initialized memory.
THANK YOU