Topic 2.1: What are Strings?
string is an array of characters, here's
• A string is a data type used in how you declare a string:
programming, such as an integer and
floating-point unit, but is used to
represent text/character rather than
numbers.
• It is comprised of a set of Topic 2.3: Initializing Strings
characters treated as a single unit
that can also contain spaces and
numbers.
• Typically, programmers must enclose
strings in quotation marks for the
data to recognized as a string and not a
number or variable name.
• The following is a pictorial
Option1 and Option2 may be variables
presentation of the defined string in C:
containing integers, strings, or other
data. If the values are the same, the
test returns a value of true, otherwise • The C compiler automatically
the result is false. In the comparison: places the '\0' at the end of the
string when it initializes the array.
Option1 and Option2 are being treated Topic 2.4: Printing/Reading Strings
as strings. Therefore, the test is
comparing the words "Option1" and
"Option2," which would return false. The
length of a string is often determined by
using a null character.
• In C, a string is an array of
characters ending in the null
character (‘\0’).
• When this character is placed at the
end of a character string, standard C
Reading Strings
function printf() know when the end of
• You can use the scanf() function to
the string is reached.
read a string like any other data types.
• For example:
• However, the scanf() function only
takes the first entered word. The
function terminates when it
• printf uses the %s (string)
encounters a white space (or just
conversion to print the contents of the
space).
array word.
Topic 2.2: Declaring Strings
• Strings are declared in a similar
manner as arrays. Only difference is
that, strings are of char type.
• Before you can work with strings, you
need to declare them first. Since
standard library function
scanf()
getchar() Topic 2.5: String Functions
• Here are some of the commonly used
string functions and are defined in the
‘#include<string.h>’ header file/string
library:
• strlen()
• strcpy()
• strcmp()
• strcat()
• strlen() function takes a single
argument, i.e, the string variable whose
length is to be found, and returns the
length of the string passed.
• Take note that this returns an integer the concatenated string in the first
value. string specified in the argument, and
the second string remains unchanged.
• strcpy() function copies the string
• Keep in mind that strcat modifies the
pointed by source (including the null
original string (str1 in this case) in
character) to the character array
place by appending the content of the
destination, which returns the character
second string (str2).
array destination.
• It is important to note that, the
destination array should be large
enough otherwise it may result in
undefined behavior.
• The strcmp() compares two strings
character by character. If the first
character of two strings are equal, next
character of two strings are compared.
This continues until the corresponding
characters of two strings are different or
a null character '\0' is reached.
• It takes two arguments, i.e, two
strings or character arrays, and stores