[go: up one dir, main page]

0% found this document useful (0 votes)
15 views32 pages

7 Strings

Strings

Uploaded by

pratiksngh1706
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views32 pages

7 Strings

Strings

Uploaded by

pratiksngh1706
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

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


• 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


• 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


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

Programming with Sikander


#define MAX_BUFFER 20
No Need of &
char buffer[MAX_BUFFER];
scanf(“%s”, buffer); // or
gets(buffer);
• 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


#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
Try with input
RAJU
SIKANDER
Programming with Sikander
fgets also read \n as part of input

Programming with Sikander


 scanf(“ %[^\n]s”, str);
 fgets(str, SIZE, stdin);

Programming with Sikander


char buffer[] = “PROGRAMMING WITH SIKANDER”;

printf(“%s”, buffer); // or
puts(buffer);

prints characters up to null character

Programming with Sikander


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

Include ctype.h when using these 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 otherwise.
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.
 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
Programming with Sikander
 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


Programming with Sikander
Few Important Functions for Manipulating
Strings : Unrestricted
Function Name Function Description
strcpy(Target_String_Var,Sr Copies the string value Src_String into the string variable Target_String_Var.
c_String)
strcat(Target_String_Var,Sr Concatenates the string value Src_String onto the end of the C-string in the
c_String) 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).

String library functions.


Include string.h when using these functions
Programming with Sikander
Programming with Sikander
Functions for Manipulating Strings : Restricted

Function Name Function Description


strncpy(Target_String_Var, The same as the two-argument strcpy except that at most Limit characters are
Src_String, Limit) copied.
strncat(Target_String_Var, The same as the two argument strcat except that at most Limit characters are
Src_String, Limit) appended.
strncmp(String_1,String_2, The same as the two-argument strcat except that at most Limit characters are
Limit) compared.

String library functions.


Include string.h when using these functions
 char name[20]; // Store 1 name
 char names[NO_NAMES][MAX_LEN]; // Store NO_NAMES
with each name upto MAX_LEN characters

 char names[5][20]; //Stores 5 names with each name of 20


characters
 char name[5][20];
 for(i = 0 ; i < 5 ; i++)
printf(“%s”,names[ i ]);
THANK YOU

Programming with Sikander

You might also like