Introduction

Strings in programming are similar to character arrays.

Strings are used for text manipulation.

A string is a one-dimensional array of characters terminated by a null character '\0'.

String Initialization

Strings can be initialized using character arrays.

Example: char name[] = {'H', 'A', 'E', 'S', 'L', 'E', 'R', '\0'};

'\0' indicates the end of a string.

String Initialization Shortcut

C provides a shortcut for string initialization.

Example: char name[] = "HAESLER";

'\0' is automatically added by C.

String Manipulation

You can access string elements like a character array.

Example: Print a string character by character using a loop.

String Manipulation (Contd.)

You can use a pointer to access string elements.

Example: Use a pointer to iterate through the string.

String Printing with printf

printf() can print entire strings.

Example: printf("%s", name);

%s is used for string formatting.

Reading String from Input

You can use scanf() to read strings from input.

Example: char name[25]; scanf("%s", name);

Make sure the string fits within the array bounds.

Reading Multi-Word Strings

scanf() cannot read multi-word strings.

Use gets() to read multi-word strings.

Example: char name[25]; gets(name);

Standard Library String Functions

C provides various string handling library functions.

Functions like strlen, strcpy, strcat, and strcmp.

These functions simplify string manipulation.

strlen() Function

strlen() calculates the length of a string.

Example: int len = strlen("Hello");

It does not count the '\0' character.

strcpy() Function

strcpy() copies one string into another.

Example: char dest[20]; strcpy(dest, "Source");

strcat() Function

strcat() concatenates one string to the end of another.

Example: char dest[30] = "Hello"; strcat(dest, " World");

strcmp() Function

strcmp() compares two strings.

Returns 0 if strings are identical.

Example: int result = strcmp("Jerry", "Ferry");

Custom strcmp() Function

Implement a custom version of strcmp() for string comparison.

Compare strings and return a result based on alphabetical order.

Conclusion

Strings in C are character arrays with a null character '\0'.

Standard library functions simplify string manipulation.

Custom functions can be created for more specific needs.