Introduction To Strings in C
Introduction To Strings in C
Introduction To Strings in C
Strings are a fundamental data type in the C programming language, used to represent and manipulate textual information. In this
introduction, we'll explore the basics of defining, initializing, and working with strings in C.
UID: 24BCS12673
Defining Strings in C
3 Null Terminator
The end of a string is marked by the null character '\0', which is
automatically added by the compiler.
String Initialization and Assignments
Static Initialization Dynamic Initialization String Assignments
Strings can be initialized during Strings can also be initialized at String values can be assigned to other
declaration using double quotes, e.g., runtime using functions like strcpy() or string variables using the assignment
char name[] = "John"; strncpy(). operator = or string functions.
String Manipulation Functions
1 Concatenation
The strcat() and strncat() functions allow you to combine
two strings.
2 Length
The strlen() function returns the length of a string,
excluding the null terminator.
Copying
3
The strcpy() and strncpy() functions can be used to copy
the contents of one string to another.
String Input and Output
Console Input Console Output
The scanf() function can be The printf() function can be
used to read strings from the used to print strings to the
console, but it has limitations. console, with optional
formatting.
File I/O
Functions like fgets() and fputs() allow for reading and writing strings
to files.
String Comparison Operations
Equality Case-insensitive
The strcmp() function compares two strings and returns an The stricmp() and strnicmp() functions provide case-
integer indicating their relationship. insensitive string comparison.
1 2 3
Partial Comparison
The strncmp() function compares a specified number of
characters between two strings.
Common String Algorithms
String Search
The strstr() function can be used to find the first occurrence of a substring
within a string.
String Reversal
The strrev() function reverses the order of characters in a string.
String Tokenization
The strtok() function splits a string into tokens based on a specified
delimiter.
Memory Management and
Strings
Dynamic Allocation
Deallocation
Resizing