C does nothave a String type to easily
create string variables. Instead, you must
use the char type and create an array of
characters to make a string in C
WHAT ARE
STRINGS?
Strings are used for storing text/characters.
For example, "Hello World" is a string of
characters.
6.
OUTPUT THE
STRINGS
To outputthe string, you can use the printf()
function together with the format specifier
%s to tell C that we are now working with
strings:
Note that you have to use
double quotes (" ")
7.
To use them,you must include the
<string.h> header file in your program.
STRING
FUNCTIONS
C also has many useful string functions,
which can be used to perform certain
operations on strings.
8.
STRINGS - SPECIALCHARACTERS
Because strings must be written within quotes, C will
misunderstand this string, and generate an error:
The solution to avoid this problem, is to use the
backslash escape character.
The backslash () escape character turns special
characters into string characters.
STRING MANIPULATION FUNCTIONS
Stringmanipulation functions are a set of operations
or functions provided by programming languages
and libraries to perform various tasks on strings.
These tasks can include tasks such as finding the
length of a string, copying one string to another,
concatenating strings, comparing strings, searching
for substrings, and more.
12.
USE STRLEN() TOFIND THE LENGTH OF A
STRING?
In C and C++ (and some other languages), you can
use the strlen() function to find the length of a null-
terminated string. Here's how to use it:
13.
This code snippetincludes the <string.h> header,
which provides the strlen() function. It calculates
the length of the string myString (including all
characters except the null terminator) and stores it
in the length variable.
14.
USE STRCPY() TOCOPY ONE STRING TO
ANOTHER
In C and C++, you can use the strcpy() function to
copy one string to another. Here's how to use it:
15.
The <string.h> headeris again included, and
strcpy() is used to copy the content of the source
string into the destination string. Make sure that
the destination array is large enough to
accommodate the source string, including the
READ A STRINGFROM THE USER
There are two main ways to read a string from the
user in a C program: Using the scanf() function &
Using the fgets() function. Which method you choose
to use depends on your specific needs. If you only
need to read a single line of input from the user,
then the fgets() function is a good option. If you
need to read multiple lines of input from the user,
then the scanf() function is a better option.
18.
DISPLAY A STRINGON THE
SCREEN
This variable will store the string that you want to
display. The string variable must be declared as an
array of characters, with enough space to store the
string plus the null character (0). You can also use
the puts() function to display a string on the screen.
The puts() function takes a pointer to a string as its
argument and prints the string to the standard
output device
STRING COMPARISON
String comparisonis a fundamental operation in
programming, allowing us to determine the equality
or ordering of strings. These functions play a crucial
role in various programming languages and can be
used to perform powerful string manipulations.
22.
WHAT ARE STRINGCOMPARISON
FUNCTIONS?
String comparison functions are built-in functions in
programming languages that enable us to compare
strings. They typically return an integer value
indicating the relationship between two strings. The
result can be used to determine if the strings are
equal, or if one string is greater or lesser than the
other based on lexicographical order.
23.
USE STRCMP() TOCOMPARE TWO
STRINGS
strcmp() - Comparing two strings: The strcmp()
function is widely used for string comparison. It
takes two string arguments and returns an integer
value.
24.
USE STRCMP() TOCOMPARE TWO
STRINGS
The strcmp() function is widely used for string
comparison. It takes two string arguments and
returns an integer value. If the two strings are equal,
strcmp() returns 0. If the first string is
lexicographically less than the second string,
strcmp() returns a negative value .If the first string is
lexicographically greater than the second string,
strcmp() returns a positive value.
DECLARE A STRING
Declaringa string in C is as simple as declaring a
one-dimensional array.
in the above syntax str_name is any name given
to the string variable and size is used to define
the length of the string, i.e the number of
characters strings will store.
28.
INITIALIZE A STRING
Astring in C can be initialized in different ways.
Assigning a string literal without size, assigning a
string literal with a predefined size, assigning
character by character with size, Assigning character
by character without size.
29.
DIFFERENCE BETWEEN DECLARATIONAND
INITIALIZATION
In C, a declaration of a string is a statement that
introduces the name of the string to the compiler,
along with information about the type of data it will
hold. It does not allocate memory for the string.
On the other hand, initialization of a string is the
process of assigning an initial value to a variable at
the time of declaration.
It allocates memory for the string and initializes it wi
th a given value
30.
CONCLUSIONS
C Strings arean essential part of the C
programming language. C Strings can be
declared, initialized, read, passed, and
manipulated using various functions and
operators. However, C Strings also have
some limitations, such as the lack of a built-
in string type, the risk of buffer overflow, and
the difficulty of handling Unicode characters.