Strings in C - covers string functions

MohammedSikander 48 views 44 slides Apr 02, 2025
Slide 1
Slide 1 of 44
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36
Slide 37
37
Slide 38
38
Slide 39
39
Slide 40
40
Slide 41
41
Slide 42
42
Slide 43
43
Slide 44
44

About This Presentation

String in C


Slide Content

Strings Programming with Sikander

Objectives In this chapter you 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

Introduction There is no 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

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

Programming with Sikander | C Programming | Strings

Programming with Sikander | C Programming | Strings

Programming with Sikander | C Programming | Strings

Common Programming Errors char subject[4]=“RTOS”; // No space for storing null character Programming with Sikander | C Programming | Strings

Reading String No Need of & 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

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 Try with input RAJU SIKANDER Programming with Sikander | C Programming | Strings

fgets fgets also read \n as part of input Programming with Sikander | C Programming | Strings

Reading multiword string scanf (“ %[^\n]s”, str); fgets (str, SIZE, stdin); Programming with Sikander | C Programming | Strings

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

Functions for Manipulating Strings 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

Useful Character-handling library functions. Prototype Function 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

Write a Program to read a string and i . count number of digits ii. Count number of Uppercase alphabets iii. Count number of Lowercase alphabets

Program to count number of digits, uppercase and lowercase characters. Programming with Sikander | C Programming | Strings

Write a Program 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

Function to convert lower case to uppercase and vice versa Programming with Sikander | C Programming | Strings

String library functions. Include string.h when using these functions Function Name Function Description strcpy ( Target_String_Var,Src_String ) Copies the string value Src_String into the string variable Target_String_Var. strcat ( Target_String_Var,Src_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

strlen - calculate the 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').

strlen - calculate the length of a string Programming with Sikander | C Programming | Strings

strlen - calculate the length of a string Programming with Sikander | C Programming | Strings

strcpy – copy a 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

strcpy – copy a String Programming with Sikander | C Programming | Strings

Comparing String Programming with Sikander | C Programming | Strings

strcmp – compare two 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

strcmp – compare two strings Programming with Sikander | C Programming | Strings

strcmp – compare two strings Programming with Sikander | C Programming | Strings

strcmp – compare two strings Programming with Sikander | C Programming | Strings

strcat – concatenate two 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.

Programming with Sikander | C Programming | Strings

Programming with Sikander | C Programming | Strings After strcpy After strcat

String library functions. Include string.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

strncmp

Storing Multiple strings char name[10]; // Store 1 name of max 9 characters char names[5][10]; // Store 5 names with each names upto 9 characters.

Storing Multiple strings

Accessing individual strings names[0] SIKANDER names[1] NIHAD

Sorting of Strings Programming with Sikander | C Programming | Strings

THANK YOU Programming with Sikander | C Programming | Strings
Tags