Strings
Strings
Introduction
o A String in C programming is a sequence of characters terminated with a null character ‘\0’.
o The C String is stored as an array of characters.
o The difference between a character array and a C string is that the string in C is terminated with a unique character ‘\0’.
o
STRING TAXONOMY
In C, we can store a string either in fixed-length format or in variable-length format as shown
Fixed-length string
o When storing a string in a fixed- length format, specify an appropriate size for the string variable.
o If the size is too small, then it will not be able to store all the elements in the string.
o On the other hand, if the string size is large, then unnecessarily memory space will be wasted.
Variable-length string
o The string can be expanded or contracted to accommodate the elements in it.
o For example, if a string variable is declared to store the name of a student. If a student has a long name of say 20 characters, then the
string can be expanded to accommodate 20 characters.
o On the other hand, a student name has only 5 characters, then the string variable can be contracted to store only 5 characters.
o However, to use a variable-length string format a technique is needed to indicate the end of elements that are a part of the string.
o This can be done either by using length-controlled string or a delimiter.
Length-controlled string
o In a length-controlled string, specify the number of characters in the string.
o This count is used by string manipulation functions to determine the actual length of the string variable.
Delimited string
o In this format, the string is ended with a delimiter.
o The delimiter is then used to identify the end of the string.
o For example, in English language every sentence is ended with a full-stop (.).
o In C any character such as comma, semicolon, colon, dash, null character, etc. are used as the delimiter of a string.
o Null character is the most commonly used string delimiter in the C language.
ARRAYS OF STRINGS
An array of string is declared as,
char names [20] [30];
The first index will specify how many strings are needed and the second index specifies the length of every individual string.
It allocates space for 20 names where each name can be a maximum of 30 characters long.
The general syntax for declaring a two-dimensional array of strings can be given
<data type> <array_name> [row_size] [column_ size];
Example:
The memory representation of an array of strings.
If we have an array declared as
char name [5] [10] = {"Ram", "Mohan", "Shyam", "Hari", "Gopal"};
By declaring the array names, we allocate 50 bytes. But the actual memory occupied is 27 bytes. Thus we see, more than half of the
memory allocated lies wasted.
Figure 6.21 shows an algorithm to process an individual string from an array of strings.
Write a program to read and print the text until a * is encountered. Also count the number of characters in the text entered.
#include <stdio.h>
#include <conio.h>
int main()
{
char str[100];
int i=0;
clrscr();
printf("\n Enter * to end");
printf("\n Enter the text: ");
scanf("%c", &str[i]);
while (str[i] != ‘*’)
{
i++;
scanf("%c", &str[i]);
}
str[i] = '\0';
printf("\n The text is: ");
i=0;
while (str[i] != '\0')
{
printf("%c", str[i]);
i++;
}
printf("\n The count of characters is: %d", i);
return 0;
}
Output
Enter * to end
Enter the text: Hi there*
The text is: Hi there
The count of characters is: 8
Write a program to read a sentence. Then count the number of words in the sentence.
#include <stdio.h>
#include <conio.h>
int main()
{
char str[200];
int i=0, count=0;
clrscr();
printf("\n Enter the sentence: ");
gets (str);
while (str[i] != '\0')
{
if (str[i] == && str[i+1] != ` ')
count++;
i++;
}
printf("\n The total count of words is: %d", count+1);
return 0;
}
Output
Enter the sentence: How are you
The total count of words is: 3
Write a program to read multiple lines of text until a* is entered. Then count the number of characters,words, and lines in the text.
#include <stdio.h>
#include <conio.h>
int main()
{
char str[200];
int i=0, word_count = 0, line_count =0, char_count = 0;
clrscr();
printf("\n Enter a * to end");
printf("\n Enter the text: ");
scanf("%c", &str[i]);
while (str[i] != '*')
{
i++;
scanf("%c", &str[i]);
}
str[i] = '\0';
i=0;
while (str[i] != '\0')
{
if (str[i] =='\n' || i==79)
line_count++;
if (str[i] = '' && str[i+1] !=` ')
word_count++;
char_count++;
i++;
}
printf("\n The total count of words is: %d", word_count+1);
printf("\n The total count of lines is: %d", line_count+1);
printf("\n The total count of characters is: %d", char_count);
return 0;
}
Output
Enter the text: Hi there*
The total count of words is: 2
The total count of lines is: 1
The total count of characters is: 8