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:

// C String Program – String Functions
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char str1[15], str2[15], str3[20];
clrscr();
printf(“\n Enter the first string: “);
scanf(“%s”,str1);
printf(“\n Enter the second string: “);
scanf(“%s”,str2);
printf(“\n Show First string length: %d”,strlen(str1));
printf(“\n Show Frist string Lower case: %s”,strlwr(str1));
printf(“\n Show First string Upper case: %s”,strupr(str1));
printf(“\n Concatenate two String: %s”,strcat(str1,str2));
printf(“\n Show string Reverse order: %s”,strrev(str1));
printf(“\n Copy string str1 to str3: %s”,strcpy(str3,str1));
getch();
}
O/P: 
Enter the first string: Ram
Enter the second string: Sham
Show First string length: 3
Show Frist string Lower case: ram
Show First string Upper case: RAM
Concatenate two String: RAMSham
Show string Reverse order: mahSMAR
Copy string str1 to str3: mahSMAR

Some More: 

POP- Introduction to Programming Using ‘C’

DS – Data structure Using C

OOP – Object Oriented Programming 

Java Programming

DBMS – Database Management System

RDBMS – Relational Database Management System

Join Now: Data Warehousing and Data Mining 

Leave a Reply

Your email address will not be published. Required fields are marked *