All About Strings Overview of String Operations in Programming
Introduction to Strings • Strings are sequences of characters used to store text. • They are widely used in programming for handling text data. • Common use cases include text processing, data manipulation, and user input handling.
Declaring and Initializing String Variables • String declaration involves specifying the variable type and name. • Initialization assigns a value to the string variable. • Example: char str[50]; // Declaration strcpy(str, "Hello"); // Initialization
Input/Output Functions for Strings • Input functions like scanf() and gets() are used to read strings. • Output functions like printf() and puts() are used to display strings. • Example: char name[50]; gets(name); // Read input puts(name); // Display output
Standard Library String Functions • C programming provides a library of standard functions for string manipulation. • These functions include operations like copying, concatenating, comparing, and finding the length of strings.
Common String Functions • strlen() - Returns the length of a string. • strcat() - Concatenates two strings. • strcpy() - Copies one string to another. • strcmp() - Compares two strings. • strrev() - Reverses a string. • strupr() - Converts a string to uppercase. • strlwr() - Converts a string to lowercase.
Passing Strings to Functions • Strings can be passed as arguments to functions. • The function can then manipulate or use the string as needed. • Example: void printString(char str[]) { printf("%s", str); }
Array of Strings • An array of strings is a collection of strings stored together. • Useful for handling multiple pieces of text data, such as names or commands. • Example: char names[3][20] = {"Alice", "Bob", "Charlie"};