Strings in C
A string in C is a sequence of characters terminated by a null character '0'
. Strings are used to represent text in C programming, and they are implemented as arrays of characters. Strings are widely used for tasks like input/output, text processing, and data manipulation.
Definition
A string in C is an array of characters that is terminated by the special character '0'
to indicate the end of the string. Strings can be manipulated using string handling functions like strlen()
, strcpy()
, strcat()
, etc.
String Declaration and Initialization
In C, strings can be declared in two ways:
- Using character arrays.
- Using string literals enclosed in double quotes.
Syntax:
char str1[20]; // Declaration of character array
char str2[] = "Hello, World!"; // Initialization with a string literal
Example: Declaring and Printing a String
This example demonstrates how to declare a string and print it using the printf()
function.
Example Code:
#include <stdio.h>
int main()
{
char str[] = "Hello, C World!";
printf("String: %s\n", str); // Using %s to print the string
return 0;
}
Output
String Length
In C, the length of a string can be calculated using the strlen()
function, which counts the number of characters in the string excluding the null character '0'
.
Example: Calculating String Length
#include <stdio.h>
#include <string.h>
int main()
{
char str[] = "Hello, C!";
int length = strlen(str); // strlen() function calculates string length
printf("Length of string: %d\n", length);
return 0;
}
Output
String Manipulation Functions
C provides several functions to manipulate strings. Below are some common string functions:
- strcpy(destination, source): Copies the source string into the destination string.
- strcat(destination, source): Concatenates (appends) the source string to the destination string.
- strcmp(str1, str2): Compares two strings and returns 0 if they are equal, a positive value if
str1
is greater, and a negative value ifstr1
is smaller thanstr2
. - strchr(str, character): Returns a pointer to the first occurrence of the specified character in the string.
- strstr(str, substring): Finds the first occurrence of a substring within a string.
Example: Using strcpy and strcat
This example demonstrates the use of strcpy()
and strcat()
functions to copy and concatenate strings.
Example Code:
#include <stdio.h>
#include <string.h>
int main()
{
char str1[20] = "Hello, ";
char str2[] = "World!";
// Copying str2 to str1
strcpy(str1, str2);
printf("After strcpy: %s\n", str1);
// Concatenating str1 with str2
strcat(str1, " C!");
printf("After strcat: %s\n", str1);
return 0;
}
Output
After strcat: World! C!
Key Points
- Strings in C are arrays of characters terminated by the null character
'0'
. - String manipulation functions like
strcpy()
,strcat()
, andstrcmp()
are part of the C Standard Library. - To determine the length of a string, use the
strlen()
function, which excludes the null terminator. - When dealing with strings, ensure that there is enough space in the array to store the string and the null character.
- Always terminate strings with
'0'
to avoid undefined behavior.