You are on page 1of 19

Lecture

 String in C
What are Strings

 The way a group of integers can be stored in an integer array,


similarly a group of characters can be stored in a character array.
Character arrays are also called strings. Many languages internally
treat strings as character arrays, but some how conceal this fact
from the programmer. Character arrays or strings are used by
programming languages to manipulate text such as words and
sentences.
 A string constant is a one-dimensional array of characters
terminated by a null ( ‘\0’ ).

 For example,

char name[ ] = { ‘N', ‘I', ‘L', ‘E', '\0' } ;


 Each character in the array occupies one byte of memory and the
last character is always ‘\0’. What character is this? It looks like two
characters, but it is actually only one character. ‘\0’ is called null
character. Note that ‘\0’ and ‘0’ are not same. ASCII value of ‘\0’ is 0,
whereas ASCII value of ‘0’ is 48.

 Note:

The terminating null (‘\0’) is important, because it is the only way the
functions that work with a string can know where the string ends. In
fact, a string not terminated by a ‘\0’ is not really a string, but merely
a collection of characters.
Example 1

main( )
{
char name[ ] = “Muhammed" ;
int i = 0 ;
while ( i <= 7)
{
printf ( "%c", name[i] ) ;
i++ ;
}
Example
main( )
{
char name[ ] = “Muhammed" ;
int i = 0 ;
while ( name[i] != `\0' )
{
printf ( "%c", name[i] ) ;
i++ ;
}
}
The %s format

 The %s used in printf( ) is a format specification for printing out a


string. The same specification can be used to receive a string from
the keyboard, as shown below.

main( )
{
char name[25] ;
printf ( "Enter your name " ) ;
scanf ( "%s", name ) ;
printf ( "Hello %s", name ) ;
}
Note
 Inputting strings
 Use of scanf
scanf("%s", word);
 Copies input into word[]

 Do not need & (because a string is a pointer)

 Remember to leave room in the array for '\0'

 scanf() is not capable of receiving multi-word strings. Therefore


names such as ‘Debashish Roy’ would be unacceptable. The way to
get around this limitation is by using the function gets( ). The usage
of functions gets( ) and its counterpart puts( ) is shown below.
Example
main( )
{
char name[25] ;
printf ( "Enter your full name " ) ;
gets ( name ) ;
puts ( name ) ;
}
 Name such as Nile University with multi-world
Execution of scanf ("%s", dept);

• Whenever scanf() encounters a white space, the scanning stops and


scanf places the null character at the end of the string.
• e.g., if the user types “MATH 1234 TR 1800,” the string “MATH” along
with ‘0’ is stored into dept.
•the string “MATH” along with ‘0’ is stored into dept.
String Library Functions

 The string can not be copied by the assignment operator ‘=’.


 e..g, “str = “Test String”” is not valid.
 C provides string manipulating functions in the “string.h” library.
 The complete list of these functions can be
found in Appendix B of the textbook.
Some String Functions from String.h
Function Purpose Example
strcpy Makes a copy of a strcpy(s1, “Hi”);
string
strcat Appends a string to the strcat(s1, “more”);
end of another string
strcmp Compare two strings strcmp(s1, “Hu”);
alphabetically
strlen Returns the number of strlen(“Hi”)
characters in a string returns 2.
strtok Breaks a string into strtok(“Hi, Chao”,
tokens by delimiters. “ ,”);
Functions strcpy
 Function strcpy copies the string in the second
argument into the first argument.
 e.g., strcpy(dest, “test string”);
 The null character is appended at the end
automatically.
 If source string is longer than the destination string, the
overflow characters may occupy the memory space
used by other variables.

9-13
Functions strcat and strlen
 Functions strcat concatenate the fist string argument with the
second string argument.
 strcat(dest, source);

9-14
Functions strcat and strlen
 Function strlen is often used to check the length of a string (i.e., the
number of characters before the fist null character).

9-15
Distinction Between Characters and Strings
 The representation of a char (e.g., ‘Q’) and a string (e.g., “Q”) is
essentially different.
A string is an array of characters ended with the
null character.

Q Q \0

Character ‘Q’ String “Q”

9-16
String Comparison (1/2)
 Suppose there are two strings, str1 and str2.
 Thecondition str1 < str2 compare the initial
memory address of str1 and of str2.
 The comparison between two strings is done by
comparing each corresponding character in them.
 The characters are comapared against the ASCII table.
 “thrill” < “throw” since ‘i’ < ‘o’;
 “joy” < joyous“;
 The standard string comparison uses the strcmp
and strncmp functions.

9-17
String Comparison (2/2)
Relationship Returned Value Example
str1 < str2 Negative “Hello”< “Hi”
str1 = str2 0 “Hi” = “Hi”
str1 > str2 Positive “Hi” > “Hello”

• e.g., we can check if two strings are the same by


if(strcmp(str1, str2) != 0)
printf(“The two strings are different!”);

9-18
strcmp( )

Next class

You might also like