Module 2 - Part 2 - Syllabus
• Strings
• Declaring a string variable
• Reading and displaying strings
• String related library functions
• Programs for string matching.
Strings
• A String in C programming is a sequence of characters terminated with a null
character ‘\0’.
• The C String is stored as an array of characters.
• There is an extra terminating character which is the Null character (‘\0’) used to
indicate the termination of a string that differs strings from normal character
arrays.
C String Declaration
• Declaring a string in C is as simple as declaring a one-dimensional array of
character type.
char string_name[size];
eg: char str[50];
• Like in array, we can skip the size in declaration.
char string_name[];
eg: char str[];
• 2D array of strings declaration.
S2-GXEST 204-PROGRAMMING IN C (2024 Scheme) Reenu Renjith - Assistant Professor CSE - MBITS 1
, char string_name [] [];
eg: char str[5][50];
//creates an array of 5 strings, with each string that has a capacity of
50 characters.
C String Initialization
• Assigning a String Literal without Size
char str[] = "GeeksforGeeks";
• Assigning a String Literal with a Predefined Size
char str[50] = "GeeksforGeeks";
• Assigning Character by Character with Size
char str[14] = { 'G','e','e','k','s','f','o','r','G','e','e','k','s','\0'};
• Assigning Character by Character without Size
char str[] = { 'G','e','e','k','s','f','o','r','G','e','e','k','s','\0'};
• Array of strings initialization
char str_arr[4][50] = {“mango”, “banana”, “apple”, “pear”);
// C program to illustrate strings
#include <stdio.h>
#include <string.h>
int main()
{
// declare and initialize string
char str[] = "Geeks";
// 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;
}
S2-GXEST 204-PROGRAMMING IN C (2024 Scheme) Reenu Renjith - Assistant Professor CSE - MBITS 2
, Output
Geeks
Length of string str is 5
Reading and displaying strings
• Reading strings :
o Using scanf()
o Here, the string is read only till the whitespace is encountered.
o Using fgets()
o The fgets() function reads content from the file up to the next line
break and writes it into a char array.
o It stops reading when either (num-1) characters have been read, a
newline is encountered, or the end-of-file is reached, and it
automatically appends a null character to terminate the string.
o Syntax
▪ fgets(str, sizeof(str), stdin);
• where str is the char array to which the string is stored.
• Sizeof(str) denotes the maximum number of characters
to read.
• stdin denotes the standard input stream.
• Displaying strings :
o Using printf()
printf("%s",str);
o Using puts()
puts(str);
// C program to read string from user using scanf() and display it back to
user.
#include<stdio.h>
int main()
{
// declaring string
char str[50];
printf("Enter the string: ");
// reading string without & symbol
scanf("%s",str);
S2-GXEST 204-PROGRAMMING IN C (2024 Scheme) Reenu Renjith - Assistant Professor CSE - MBITS 3