Chapter 6 Strings
Chapter 6 Strings
continues
Chapter -6
STRINGS
Definition:
• String is a one dimensional array of characters.
(or)
• String is a collection of characters or sequence
of characters enclosed in double quotes.
• Example= “computer”
• This string is stored in memory as shown
below
C O M P U T E R \0
Declaration of a string
• Like arrays strings must be declared before they
appear in a c program.
• Syntax:
datatype stringname[size] ;
Ex: char name [10] ;
• Initialization:
Syntax:
datatype stringname[size] = {“string”};
Ex: char name [10] = { “Bangalore” }; (or)
char name [10] = { ‘B’, ‘a’, ‘n’, ‘g’,’a’,’l’,’o’,’r’,’e’, ‘\0’ };
Operations on strings
There are so many operations in
strings. They are
•Reading and writing the strings
•Concatenating strings
•Copying one string to another
•Comparison of two strings
•Extracting the substring from a given
string
Reading a string from terminal
• Reading string from terminal using 3 ways.
They are
• scanf ( ) function
• gets ( ) function
• getchar ( ) function
**String Handling Functions
Function Work of Function
• eg: strrev(“BANGALORE”);
• The reversed string is :
• EROLAGNAB
• strrev(“MADAM”);
• MADAM
*Character Handling Functions
• The header file for character handling
functions are <ctype.h>
• 1. isdigit( int ch);
• This function checks whether its argument is a
digit (0-9).
• It returns a true value if the argument is a digit
(0-9) and 0 otherwise.
• Eg: printf(“%d”,isdigit(‘A’); // returns 0
int isalpha(int c)
• isalpha(int c);
The function returns a 1 if c is alphabetic ,
otherwise returns 0
• Eg: printf(“%d”,isalpha(“A”); // returns 1
isalnum( )
• isalnum( int ch);
• This function checks whether its argument is a
digit (0-9) or letter (a-z)/(A-Z). Letter may be an
uppercase or a lowercase.
• It returns a 1 value if the argument is a digit (0-
9) or letter (a-z)/(A-Z) and zero (0) otherwise.
• Eg: printf(“%d”,isalnum(‘A’); // returns 1
• printf(“%d”,isalnum(‘#’); // returns 0
islower()
• islower()
function checks whether its argument is a
lowercase character (a-z) or not.
• Eg:printf(“%d”,islower(‘T’); // returns 0
• printf(“%d”,islower(‘t’); // returns 1
isupper( )
• tolower( )
• tolower converts an uppercase letter (A-Z) to a
lowercase letter (a-z).
• Eg: printf(“%d”,tolower(‘B’); // returns b
toupper( )
• toupper( )
• toupper( ) converts an lowercase letter (A-Z)
to a lowercase letter (a-z).
• Eg: printf(“%d”, toupper(‘b’); // returns B
isspace( )