0% found this document useful (0 votes)
26 views22 pages

Chapter 6 Strings

Uploaded by

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

Chapter 6 Strings

Uploaded by

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

Unit -2

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

strlen() Calculates the length of string

strcpy() Copies a string to another string

strcat() Concatenates(joins) two strings

strcmp() Compares two string

strlwr() Converts string to lowercase

strupr() Converts string to uppercase

strrev To reverse a string


• Strings handling functions are defined
under "string.h" header file.
#include <string.h>
*Commonly used string functions
• strlen() - calculates the length of a string
• strcpy() - copies a string to another
• strcmp() - compares two strings
• strcat() - concatenates two strings
• strrev()- reverse the string
• strupr() – convert the letters to uppercase
• strlwr() – convert the letters to lowercase

Length of String (strlen( ))
• This function returns the number of characters in
given string. That is the string length. String length
does not include the null character (\0).
Syntax:
• strlen (string);
Where string is one dimensional array of characters.
• Ex: strlen (“programming”);
• Output: length of the string will be 11.
Ex: strlen (“india is great”);
• Output: length of given string will be 14 with
including spaces.
(strcpy ( ) function)
•Copying one string to another string
This function is used to copy the contents
of the second string to the first string
Syntax:
strcpy( string1, string2 )
Eg: strcpy(name,Arun)
o/p
name=Arun
Concatenating Operation (strcat( ) )
• The process of combining two strings together is
known as Concatenation. (or)
• The process of joining two strings into one string is
known as concatenation.
• For this we use one string handling function that is
strcat ( ).
• Syntax:
• strcat( string1, string2);
• Here, the string2 is appended to string1.
• Ex: strcat( “C”, “Programming”);
• Output: Cprogramming
Comparing two Strings(strcmp())
• This function compares two strings character by character
and returns one of the three values { -1, 0 , 1}.
• This function returns 0 (zero), if both strings are equal.
• This function returns -1 , if the ASCII value of first string is
less than that of second string.
• This function returns 1, if the first string is greater than
second string.

• Syntax: strcmp( string1 , string2);
• Eg:= strcmp(“their”,”there”);
• Will return a positive or negative integer
• strcmp(“Hello”,”Hello”);
• will return 0 as both the strings are equal.
String lower and String upper
(strlwr( ) and strupr( ) )
strlwr( ) function is used to convert uppercase string
to lowercase.
Syntax:
Strlwr(string);
eg: strlwr(“HELLO”);
returns
hello
strupr( ) function is used to convert lowercase string
to uppercase
eg: strupr(“hello”);
returns
HELLO
String Reverse ( strrev ( ) )
• This function is used to reverse a string.
• Syntax:
strrev(string);

• 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( )

• isupper() - function checks whether its


argument is a uppercase character (a-z) or
not.
• Eg:printf(“%d”,isupper(‘T’); // returns 1
• printf(“%d”,isupper(‘t’); // returns 0
tolower()

• 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( )

• This function checks whether its argument is a


whitespace character or not.
• It returns a true value if ch is a whitespace
character and zero (0) otherwise.
• Eg: printf(“%d”, isspace(‘b’); // returns 0

You might also like