Types of Strings in C
Strings in C
A string in C programming is a group of characters that finish with the null character "0." The characters from the C String are kept in a character array. The distinguishing letter "0" comes after a C string, which sets it apart from a character array.
A String in C is nothing more than a group of characters. Creating a string in C is as easy as creating an array of one dimension.
Syntax:
The fundamental syntax for declaring a string is shown below.
char s[len];
//character string of length len
In the syntax above, str_name can be any name assigned to the string variable, and size is used to specify the string's length or the number of characters it will hold.
- Strings have an additional terminating character called the Null character ('\0') to distinguish strings from standard character arrays.
- 'C' interprets a string as a single data if it contains whitespaces.
- Single quotation representation is used to define a single character.
- To denote a string, use double quotation marks.
Declaration of String:
Here's how you can declare strings:
Char sc[5];
Initialization of String :
There are various initialization methods for strings in C.
We'll give an example to demonstrate this. Following are examples of declaring a string with the name str and initializing it with "my article": String initialization techniques in C.
We can initialize a C string using one of four different techniques, which are as follows:
String Initialization Techniques:
1. Assigning an empty string literal to a string:
String literals can have any size allocated to them. Since the string str is an array, its name here functions as a pointer.
char s[]="myarticle";
2. Giving a string a literal size that is predetermined:
A specified size can be assigned to string literals. But we must always account for the extra space the null character will add. Always define a string with a size equal to or higher than n+1 if you want to store a string of size n.
char str[50] = "myarticle";
3. Assigning character by character with size :
A string can also be assigned character per character. But we must remember to set the null character "\0" as the final character.
Char str[10] = { 'm', 'y', 'a', 'r', 't', 'i', 'c', 'l', 'e', '\0'};
4. Character-by-character assignment without regard to size:
With the NULL character at the end, we can allocate characters one at a time without regard to size. The compiler automatically determines the string's length.
'm', 'y', 'a', 'r', 't',' i', 'c', 'l', 'e', '\0';
Note: By default, anytime the compiler comes across a string of characters enclosed in double quotation marks, it appends the null character "0" to the end of the string.
Reading Strings:
1.Reading strings using gets():
- The most common method for reading strings is utilizing the library function gets().
- The "stdio.h" library contains a definition for the gets() function.
- The only parameter for the gets() function is the start address of a memory region that can hold the input.
- The gets() function extracts a null-terminated string from an input stream line terminated by the newline character n. Data is read until a new line or the file's end is encountered.
The declaration of the gets() function is,
char* gets(char* stripper);
#include <stdio.h>
#define NUM 20
int main()
{
// defining variable
char var[san];
printf("Enter string: ");
// using gets to take a string from stdin
gets(san);
printf("string is: %s\n", san);
return 0;
}
Output:
![Types of Strings in C](https://static.tutorialandexample.com/c/types-of-strings-in-c1.png)
2.Reading Strings Using fgets():
Additionally defined in the "stdio.h" library is the fgets() function.
Additionally, it creates a null-terminated string from an input stream line that is newline-terminated.
#include <stdio.h>
int main()
{
char name[30];
printf("Enter name: ");
fgets(name, sizeof(name), stdin); // read string
return 0;
}
Output:
![Types of Strings in C](https://static.tutorialandexample.com/c/types-of-strings-in-c2.png)
3.Reading Strings Using scanf():
- There are several limits on how the string can be read when utilizing the scanf() method and the %s conversion.
- Since a string is already a pointer, an address operator is not needed to read it.
- The address operator (&) can read strings, although it's not required. If we employ it, no error happens.
- The scanf() function reads the first word; it stops when a blank space appears. Our attempt to read "Know Programme" will result in "Know" becoming read-only.
- There must be sufficient room to read the string. The program will continue unless more room is available.
- Use scanf() with the %c conversion, which may have a count attached, as an alternative approach to entering strings. The newline character is not recognized as unique by this conversion. The count defines how many characters must be entered. Contrary to %s and %[] conversions, the %c conversion does not automatically produce the string terminating NULL, and if the incorrect amount of characters is provided, odd effects will be noted.
#include <stdio.h>
int main()
{
char name[20];
printf("Enter Name: ");
scanf("%s", name);
printf("Your name is %s.", name);
return 0;
}
Output:
![Types of Strings in C](https://static.tutorialandexample.com/c/types-of-strings-in-c3.png)
4.Passing Strings to a function:
We can pass strings to functions similarly to an array to a function since strings are character arrays.
Below is a sample program to accomplish this:
#include <string.h>
void print(char s[])
{
printf("The String is:%s", s);
}
int main()
{
//declare and set the string variable
char s[] = "myarticle";
//Print string by passing a string
//Send a string to a separate function to print it.
print(s);
return 0;
Output:
![Types of Strings in C](https://static.tutorialandexample.com/c/types-of-strings-in-c4.png)