In this article String in C we give the information about String is a sequence of characters and it is terminated by null (‘\0’). Strings are always kept inside double quotes.
String in C
In C programming, String is a sequence of characters and it is terminated by null (‘\0’).
In other words, “String is a one-dimensional array of characters that is terminated by null.”
The last character of String should always be null (‘\0’). This shows where the string is ending.
Strings are always kept inside double quotes. While the character is kept inside the single quote.
Each character in the array takes up one byte of memory.
For example – char c [] = “yash”;
Declare string in C:
A string is a simple array whose data type is char. The general syntax to declare it is as follows:-
char str_name[size];
In the above syntax, str_name is the name given to the string variable and size is used to define the length of the string. The number of characters that can be stored in a string will be the size of it.
Initialize String in C:
There are many ways to initialize a string. An example has been given below with the help of which you can understand it easily. In this example there is a string named str and it is initialized with “Rajveer”.
The char str[] = “Rajveer”;
The char str[50] = “Rajveer”;
char str [] = {‘R’,’a’,’j’,’v’,’e’,’e’,’r’,’\ 0′};
char str [50] = {‘R’,’a’,’j’,’v’,’e’,’e’,’r’,’\ 0′};
Below you are given the memory representation of the string “Rajveer”.
C String Program:-
#include<stdio.h>
#include<conio.h>
void main()
{
// declare and initialize string
char str[50] = “easyconcept.in”;
// print string
printf(“%s”,str);
getch();
}
O/P:easyconcept.in
C String Functions –
String.h file header in C language supports all the string functions. All string functions are given below:-
Function | Description |
strcat(s1, s2) | This concatenates string s2 at the end of string s1. (Concatenate means to add.) |
strcpy(s1, s2) | This copies s2 into s1. |
strlen(s1) | It returns the length of s1. |
strcmp(s1, s2) | It returns 0 if s1 and s2 are equal. And returns 1 when s1<s2 or s1>s2. |
strdup() | Creates a duplicate of the string. |
strlwr() | It converts the string to lowercase. |
strupr() | Converts the string to uppercase. |
strrev() | It reverses the string. |
strupr() | Converts the string to uppercase. |
strrev() | It reverses the string. |
C String Program:
Some More:
POP- Introduction to Programming Using ‘C’
OOP – Object Oriented Programming
DBMS – Database Management System
RDBMS – Relational Database Management System
Join Now: Data Warehousing and Data Mining