Strings in c
PRASHANT TOMAR
• String in C programming is a sequence of characters terminated with a
null character ‘\0’. Strings are defined as an array of characters.
• A string is a sequence of characters treated as a group.
• A string of characters is a sequence of data of type char (the ASCII
codes) stored in consecutive memory locations and terminated by the
null character ’\0’ (the ASCII value is 0). The null string (of length zero
(0)) is the null character only.
• A string is a sequential collection of Unicode characters that is used to
represent text.
• Strings are maintained as arrays of characters. Representing strings in C:
• stored in arrays of characters
• array can be of any length – the string may be shorter, may not fill the array
• end of string is indicated by a delimiter, the null character ‘\0’.
A S T R I N G \0
“A STRING”
• Declaring a string is as simple as declaring a one-dimensional array. Below is the
basic syntax for declaring a string.
char str_name[size];
4 Ways to Initialize a String in C
1. Assigning a string literal without size: String literals can be assigned without
size. Here, the name of the string str acts as a pointer because it is an array.
char str[] = “HelloWorld";
2. Assigning a string literal with a predefined size: String literals can be assigned
with a predefined size. But we should always account for one extra space which will
be assigned to the null character. If we want to store a string of size n then we
should always declare a string with a size equal to or greater than n+1.
char str[50] = "AssigningAString";
3. Assigning character by character with size: We can also assign a string character
by character. But we should remember to set the end character as ‘\0’ which is a
null character.
char str[16] = { ‘A’, ‘s’, ‘s’, ‘i’, ‘n’, ‘i’, ‘n’, ‘g’, ‘A’, ‘S’, ‘t’, ‘r’, ‘i’, ‘n’, ‘g’, ‘\0’};
4. Assigning character by character without size: We can assign character by
character without size with the NULL character at the end. The size of the string is
determined by the compiler automatically.
// C program to illustrate strings
#include <stdio.h>
#include <string.h>
int main()
{
// declare and initialize string
char str[] = “Prashant";
// print string
printf("%s\n", str);
int length = 0;
length = strlen(str);
// displaying the length of string
printf("Length of string str is %d", length);
return 0;
String Literals
• String literal values are represented by sequences of characters between double
quotes (“”)
• Examples
• “” - empty string
• “hello”
“a” versus ‘a’
• ‘a’ is a single character value (stored in 1 byte) as the ASCII value for the letter, a
• “a” is an array with two characters, the first is a, the second is the character
value \0
// C program to read string from user
#include<stdio.h>
int main()
{
// declaring string
char str[50];
// reading string
scanf("%s",str);
// print string
printf("%s",str);
return 0;
Passing Strings to Function
• As strings are character arrays, so we can pass strings to function in the same way
we pass an array to a function. Below is a sample program to do this:
// C program to illustrate how to pass string to functions
#include <stdio.h>
void printStr(char str[])
{
printf("String is : %s", str);
}
int main()
{
// declare and initialize string
char str[] = “HelloWorld";
// print string by passing string
// to a different function
printStr(str);
return 0;
// C Program to print Array of strings
#include <stdio.h>
// Driver code
int main()
{
char arr[3][10] = {“Test1", " Test2", “Test3"};
printf("String array Elements are:\n");
for (int i = 0; i < 3; i++)
{
printf("%s\n", arr[i]);
}
return 0;
Function Description
strlen(string_name) returns the length of string name.
strcpy(destination, source) copies the contents of source string to destination string.
strcat(first_string, concats or joins first string with second string. The result of the
second_string) string is stored in first string.
strcmp(first_string, compares the first string with second string. If both strings are
second_string) same, it returns 0.
strrev(string) returns reverse string.
strlwr(string) returns string characters in lowercase.
strupr(string) returns string characters in uppercase.