Objectives In this chapteryou will learn about • Declaring and Initializing Strings • Reading Strings from the terminal • Writing strings to screen • Character handling library functions • String handling library functions Programming with Sikander | C Programming | Strings
3.
Introduction • There isno String data type in C • String in C are simply array of characters terminated with null character (character ‘0’) • A set of characters defined within double quotation is a “string constant” Programming with Sikander | C Programming | Strings
4.
Declaring and Initializing String •Allocate space for a string just like any other array: char string[16]; • Space for string must contain room for terminating null character • we can initialize string with a string constant: • char s1[ ] = “SIKANDER"; • We can initialise a string using = but we can't set a string using this at other times; instead we need to use a function which will be discussed later • We can initialize string with a sequence of characters similar to initializing arrays • char s2[ ] = {‘S’ , ’I’ , ’K’ , ’A’ , ’N’ , ’D’ , ’E’ ,’R’}; Programming with Sikander | C Programming | Strings
Reading String No Needof & • What if the array is not large enough to hold the input? – characters will be stored into memory locations past the end of the array – will result in run-time memory access violation error ! Programming with Sikander | C Programming | Strings
10.
Reading a string: Better Approach #define MAX_BUFFER 20 char buffer[MAX_BUFFER]; fgets(buffer, MAX_BUFFER, stdin); fgets is similar to gets, but: It takes a second argument as max size it takes a third argument, a file pointer; in our case standard input it stores into buffer no more than MAX_BUFFER - 1 chars (extra characters are ignored), so memory violation error won’t occur Programming with Sikander | C Programming | Strings
fgets fgets also readn as part of input Programming with Sikander | C Programming | Strings
13.
Reading multiword string scanf(“ %[^n]s”, str); fgets(str, SIZE, stdin); Programming with Sikander | C Programming | Strings
14.
Printing String char buffer[]= “PROGRAMMING WITH SIKANDER”; printf(“%s”, buffer); // or puts(buffer); prints characters up to null character Programming with Sikander | C Programming | Strings
15.
Functions for ManipulatingStrings C provides a large number of functions for manipulating strings and characters. Character handling library Includes functions to perform useful tests and manipulations of character data Each function receives a character (an int) String handling library has functions to Manipulate string data Search strings Tokenize strings Determine string length Programming with Sikander | C Programming | Strings
16.
Useful Character-handling library functions. PrototypeFunction Description int isdigit( int c ); Returns a non-zero value if c is a digit and 0 (false) otherwise. int isalpha( int c ); Returns a non-zero value if c is a letter and 0 otherwise. int isalnum( int c ); Returns a non-zero value if c is a digit or a letter and 0 otherwise. int isxdigit( int c ); Returns a non-zero value if c is a hexadecimal digit character and 0 otherwise. int islower( int c ); Returns a non-zero value if c is a lowercase letter and 0 otherwise. int isupper( int c ); Returns a non-zero value if c is an uppercase letter and 0 other wise. int tolower( int c ); If c is an uppercase letter, tolower returns c as a lowercase letter. Otherwise, tolower returns the argument unchanged. int toupper( int c ); If c is a lowercase letter, toupper returns c as an uppercase letter. Otherwise, toupper returns the argument unchanged. Include ctype.h when using these functions
18.
Write aProgram to read a string and i. count number of digits ii. Count number of Uppercase alphabets iii. Count number of Lowercase alphabets
19.
Program to countnumber of digits, uppercase and lowercase characters. Programming with Sikander | C Programming | Strings
20.
Write aProgram to verify if the given password (string) is strong or not. A strong password consists of Atleast 1 Upper case alphabet Atleast 1 Lower case alphabet Atleast 1 digit Atleast 1 non alphanumic Programming with Sikander | C Programming | Strings
21.
Function to convertlower case to uppercase and vice versa Programming with Sikander | C Programming | Strings
22.
String library functions. Includestring.h when using these functions Function Name Function Description strcpy(Target_String_Var,Sr c_String) Copies the string value Src_String into the string variable Target_String_Var. strcat(Target_String_Var,Sr c_String) Concatenates the string value Src_String onto the end of the C-string in the string variable Target_String_Var. strlen(Src_String) Returns an integer equal to the length of Src_String. (The null character, ’0’, is not counted in the length.) strcmp(String_1,String_2) Returns 0 if String_1 and String_2 are the same. Returns a value < 0 if String_1 is less than String_2. Returns a value > 0 if String_1 is > than String_2 ( that is returns a nonzero value if String_1 and String_2 are different). Few Important Functions for Manipulating Strings : Unrestricted
23.
strlen - calculatethe length of a string Programming with Sikander | C Programming | Strings size_t strlen(const char *s); The strlen() function calculates the length of the string pointed to by s, excluding the terminating null byte (' 0').
24.
strlen - calculatethe length of a string Programming with Sikander | C Programming | Strings
25.
strlen - calculatethe length of a string Programming with Sikander | C Programming | Strings
26.
strcpy – copya String char *strcpy(char *dest, const char *src); The strcpy() function copies the string pointed to by src, including the terminating null byte ('0'), to the buffer pointed to by dest. The destination string dest must be large enough to receive the copy. Programming with Sikander | C Programming | Strings
27.
strcpy – copya String Programming with Sikander | C Programming | Strings
strcmp – comparetwo strings Programming with Sikander | C Programming | Strings int strcmp(const char *s1, const char *s2); The strcmp() function compares the two strings s1 and s2. Return value Description < 0 s1 is less than s2 = 0 s1 matches s2 (the strings are equal) > 0 s1 is greater than s2
30.
strcmp – comparetwo strings Programming with Sikander | C Programming | Strings
31.
strcmp – comparetwo strings Programming with Sikander | C Programming | Strings
32.
strcmp – comparetwo strings Programming with Sikander | C Programming | Strings
33.
strcat – concatenatetwo strings Programming with Sikander | C Programming | Strings char *strcat(char *dest, const char *src); The strcat() function appends the src string to the dest string, overwriting the terminating null byte ('0') at the end of dest. The dest string must have enough space for the result.
String library functions. Includestring.h when using these functions Function Name Function Description strncpy(Target_String_Var, Src_String, Limit) The same as the two-argument strcpy except that at most Limit characters are copied. strncat(Target_String_Var, Src_String, Limit) The same as the two argument strcat except that at most Limit characters are appended. strncmp(String_1,String_2, Limit) The same as the two-argument strcat except that at most Limit characters are compared. Functions for Manipulating Strings : Restricted