You are on page 1of 16

m 



Strings
a A string is an array of characters.
a Strings must have a 0 or null character after
the last character to show where the string
ends. The null character is not included in the
string.
a Two ways of using strings are:
The first is with a character array and
the second is with a string pointer.
a A character array is declared in the same
way as a normal array.
Example: O O
Strings

a Any sequence or set of characters defined


within double quotation symbols is a
constant string.
a Set the value of each individual element of
the array to the character you want and
you must make the last character a 0.
a Remember to use ë when printing the
string.
Strings

a In C, it is required to do some meaningful


operations on strings:
Reading string displaying strings
Combining or concatenating strings
Copying one string to another.
Comparing string & checking whether they are
equal
Extraction of a portion of a string
 
     

a The function scanf with %s format


specification is needed to read the character
string from the terminal.
Ex  
O  
 
O ë  

a O statement has a draw back it just
terminates the statement as soon as it finds a
blank space
 
     

a The function ›
O  can be used repeatedly
to read a sequence of successive single
characters and store it in the array.
a ºe cannot manipulate strings since C does not
provide any operators for string. For instance we
cannot assign one string to another directly.
Ñ x  
 › 
 ›  ›
a The example about is not valid. To copy the
chars in one string to another string we may do
so on a character to character basis.
O O
O 
O 

O 
O 
O 
O  
  ë O
a String pointers are declared as a pointer to a
char.
O  
String handling functions

a The  › header file has some


useful functions for working with strings.
a à à 
 à 
strcpy copies the source string to the
destination string.
 O
 
O  åå   
String handling functions

a à 
à 
 à 
üoins the destination and source strings and
puts the joined string into the destination
string.
 O
 
O  åå   

String handling functions

a à à à 
Compares the first and second strings. If the
first string is greater than the second one then
a number higher than 0 is returned. If the first
string is less than the second then a number
lower than 0 is returned. If the strings are
equal then 0 is returned.
 O
 O
  O  åå 
String handling functions

a à à à 
O


 will return zero because the two strings are equal.
O



 will return a 9 which is the numeric difference
between ASCII µi¶ and ASCII ¶r¶.
O


 will return 32 which is the numeric difference
between ASCII ³T´ & ASCII ³t´.
String handling functions

a à    
This function is same as strcmp() which compares
two strings but not case sensitive.
Ex 
O !

 
" 
String handling functions

a à  à  
Returns the amount of characters in a string.
 O

  
 åå 
String handling functions

a à  à  
Returns the amount of characters in a string.
 O

  
 åå 
a å Everyone¶s first C program. å
o O"
#   $
O 
 %

&'
 ()*
 %
O O
  ë ) 

›
O 
*

You might also like