Strings in C Programming
In C programming, a string is a group of words that ends with the null symbol "\0." The characters in the C String are kept as a character array. A C string differs from an element array in that the latter ends with the distinct character "\0."
Syntax of the C String Statement.
In C, defining a string is equivalent to defining an array with one dimension.
char string_name[size];
The string variable's value can be whatever is used according to the syntax previously mentioned, and its Size specifies the length of the string or the total amount of letters it can hold.
An additional termination personality, the Null symbol ('\0'), is utilized to provide the string's length or the total amount of characters it can hold.
The Null character ('\0'), an additional terminating personality, is utilized to denote the end of the string in contrast to regular character arrays.
Initializing a C String
There are various methods for initializing a string in C. We'll use an example to help clarify this.
Four methods exist for initializing a C string, and they are as follows:
1. Taking a String Literal and Ignoring Size:
String literals are assignable indefinitely. Since the string str constitutes an array, its name serves as a pointer.
char str[] = "Google";
2. Giving a Predefined Size to a String Literal:
It is possible to assign literals of strings a fixed size. However, we must always add space to accommodate a null letter. Setting up a string that is at least n+1 in Size will always be necessary when you wish to hold a length of n.
char str[50] = "Google";
3. Character Size confiding Character by Character:
A sequence of characters can also be assigned character per character. It is important to note that the final character should be set to '\0,' a blank character.
Char str[14] = { 'G',' o',' o','g', 'l', 'e','\0'};
4. Character by Character Assignment Without initialization:
Utilizing the NULL character at the very end, we can allocate characters individually without regard to Size. The compiled program determines the string's length dynamically.
Char str[] = { 'G',' o',' o','g', 'l', 'e','\0'};
Example Code:
#include <stdio.h>
#include <string.h>
int main()
{
char str[] = "Google";
printf("%s\n", str);
int len = 0;
len = strlen(str);
printf("Length of the string str is %d", len);
return 0;
}
Output:
Example Code:
To read a string from the user
#include<stdio.h>
int main()
{
char str[20];
scanf("%s",str);
printf("%s",str);
return 0;
}
Output:
The example program above demonstrates how the string may be additionally read with just one scanf command. You may also be wondering why, in the scanf declaration, we did not utilize the "&" sign alongside the string name "str"! You must review what you have learned about scanf to comprehend this.
We know that the'&' symbol is utilized to give the scanf() function the variable's location so it can save the information it reads from storage. Since str[] is a characters array, the starting point of this line can be found by using str without the braces "[" and "]". Because we already gave scanf the string's base location, we decided to utilize '&' in this instance.
Example Code:
To take input string which is separated by whitespaces
#include <stdio.h>
int main()
{
char str[20];
scanf("%s", str);
printf("%s", str);
return 0;
}
Output:
In this case, the string is just read through to the whitespace.
How do you read a string in C that is divided by whitespaces?
Several ways to read a string in C have been divided into spaces. Both of the typical ones are as follows:
- The functions fgets() and gets() can be used to read an entire line of text and characters through the input stream (stdin), respectively, and save the results as a C string until the End-of-file (EOF) or a newline character is encountered.
- Within the scanf() function, we may also scan set characters.
Length of C String
The total amount of characters in the string, excluding the NULL character, is its length. Using a looping method to count all the characters from the very start until the NULL character is encountered, we can quickly determine the total length of the string.