C strings
C strings
COLLEGE , PURULIA
C Strings
Introduction
Strings are used for storing
text/characters.
For example, "Hello World" is a char greetings[] = "Hello World!";
string of characters.
Example:
Unlike many other programming char greetings[]= "Hello World!";
Example:
char greetings[] = "Hello World!";
printf("%c", greetings[0]);
Access Strings
Example:
Since strings are actually arrays in
char greetings[] = "Hello World!";
C, you can access a string by greetings[0] = 'J';
Loop Through a
String
char str[50] = "GeeksforGeeks";
You can also loop through the
characters of a string, using a for
loop:
Assigning a String
Literal without Size
char str[14] = {
String literals can be assigned 'G','e','e','k','s','f','o','r','G','e','e','k','s','\0'};
Assigning a String
Literal with a
char str[] = {
Predefined Size 'G','e','e','k','s','f','o','r','G','e','e','k','s','\0'};
by Character without
Size
We can assign character by // C program to illustrate how to
C String Length
The length of the string is the
number of characters present in
the string except for the NULL
character. We can easily find the
length of the string using the loop
to count the characters from the
start till the NULL character is
found.
Passing Strings to
Function
As strings are character arrays, we
can pass strings to functions in the
same way we pass an array to a
function.
Conclusion
String functions in C provide a
convenient and efficient way to
manipulate strings. These
functions, such as strlen(), strcpy(),
strcat(), and strcmp(), allow for
common string operations like
finding their length, copying them,
concatenating them, and
comparing them. Careful use of
these functions can lead to more
robust and efficient programs.