INTRODUCTION
String is a set of characters that are enclosed in double quotes.
In the C programming language, strings are created using one dimension array of character
datatype.
Every string in C programming language is enclosed within double quotes and it is terminated
with NULL (\0) character.
Whenever c compiler encounters a string value it automatically appends a NULL character (\0)
at the end.
String is a set of characters enclosed in double quotation marks. In C programming, the string
is a character array of single dimension.
In C programming language, there are two methods to create strings and they are as follows...
Using one dimensional array of character datatype ( static memory allocation )
Using a pointer array of character datatype ( dynamic memory allocation )
The length of the string doesn’t include the null character.
DECLARATION The library function strlen() returns the length of this string as 5.
char c[ ] = "c string";
char greeting[6] = {'H', 'e', 'l', 'l', 'o', '\0'};
This is bad and you should never do
this - assign 6 characters (the last
character is '\0') to a char array
having 5 characters
READING STRING
We can read a string value from the user during the program execution. We use the following two
methods...
[Link] scanf() method - reads single word
[Link] gets() method - reads a line of text
Using scanf() method we can read only one word of string.
We use %s to represent string in scanf() and printf() methods.
Also notice that we have used the code name instead of &name with
scanf().
scanf("%s", name);
This is because name is a char array, and we know that array names
decay to pointers in C.
Thus, the name in scanf() already points to the address of the first
element in the string, which is why we don't need to use &.
READING STRING
To print the entire name including space by using scanf then small changes need to be done in scanf statement
scanf("%[^\n]s", s)
which instructs the compiler to store the string s while the new line (\n) is encountered.
READING STRING
You can use the gets()/fgets() function to read a line of string.
It is called an unformatted console input function, defined in the "stdio.h" header file.
In newer versions of C, gets() has been deprecated. It is potentially a
dangerous function because it doesn’t perform bound checks and may
result in buffer overflow.
The fgets() function can be used to accept input from any input
stream, such as stdin (keyboard) or FILE (file stream).
READING STRING
The following program uses fgets() and accepts multiword input from the user.
TRAVERSING A STRING
Traversing string is somewhat different from the traversing an integer array.
We need to know the length of the array to traverse an integer array, whereas we may use the
null character in the case of string to identify the end the string and terminate the loop.
Hence, there are two ways to traverse a string.
By using the length of string
By using the null character.
TRAVERSING A STRING
Printing – puts()
The following program uses puts () to display a string.
The puts() function is used to print the string on the console which is previously read by using
gets() or scanf() function.
getchar(),getch(),getche()
BUILT-IN FUNCTIONS
C programming language provides a set of pre-defined functions called string handling functions to work with
string values.
The string handling functions are defined in a header file called string.h.
Whenever we want to use any string handling function we must include the header file called string.h.
strlen()
strcpy()
strcmp()
strcmp()
strcat() and strncat()
strlwr() and tolower()
strupr() and toupper()
strstr()
• This function takes two strings s1 and s2 as arguments and finds the first occurrence of the
string s2 in the string s1.
• The process of matching does not include the terminating null-characters(‘\0’), but function
stops there.
s1: This is the main string to be examined.
s2: This is the sub-string to be searched in string.
• This function returns a pointer point to the first character of the found s2 in s1 otherwise a null
pointer if s2 is not present in s1.
• If s2 points to an empty string, s1 is returned.
strstr()
Count the Number of
Vowels
#include<stdio.h>
void main () Output
{
char s[10] =
The number of vowels 4
“appletree";
int i = 0;
int count = 0;
while(i<10)
if(s[i]=='a' || s[i] == 'e' || s[i] == 'i' || s[i] == 'u' || s[i] == 'o')
// {
count ++;
while(s[i]
} i+ !=
NULL)
+;
}{
printf
("The
numb
er of
Huffman Coding
Problem
Huffman code is a particular type of optimal prefix
code for characters. It is commonly used for lossless
data compression. It is a variable-length code derived
from frequency of occurrence. Given a string develop
an algorithm and write a C program to determine
frequency of occurrence of each character in the string.
Input Output Logic Involved
Huffman Coding problem
A string S Frequency Convert all letters to uniform
count of each case and check if it is a
letter in S particular letter and
increment corresponding
count
Algorithm
1. Read a string
2. Make all letters in the string to be in lowercase
3. Process character by character
4. If the character is an alphabet then increment
count of it
5. Print count of all alphabets
Program to find
the occurrence
of characters in
a string
Count number of characters and words in a
string in C language
Void main()
{ printf(“\n character=%d,words=
char,str[15]; %d”,c,sp+1)
int I,c=0,sp=0; }
Printf(“\n Enter the string”);
gets(str);
for (i=0;str*i+!=‘\0’;i++)
{ c+
+;
If (str*i+==‘ ‘)
sp++;
}
Count the total number of alphabets, digits
and special characters in a string
#include <stdio.h> #include <string.h> #include <stdlib.h>
#define str_size 100 // Declare the maximum size of the string int main() {
char str[str_size];
int alp, digit, splch, i;
alp = digit = splch = i = 0;
printf("\n\nCount total number of alphabets, digits, and special characters :\n");
printf(" \n");
printf("Input the string : ");
fgets(str, sizeof str,stdin); // Read a string from the standard input (keyboard)
/* Checks each character of the string */
while (str[i] != '\0') {
if ((str[i] >= 'a' && str[i] <= 'z') || (str[i]
>= 'A' && str[i] <= 'Z')) {
alp++; // Increment the alphabet
count
} else if (str[i] >= '0' && str[i] <= '9')
{ digit++; // Increment the digit
count
} else {
splch++; // Increment the special
character count
}
i++; // Move to the next character in the
string
}
// Display the counts of alphabets, digits, and special characters in the string
printf("Number of Alphabets in the string is : %d\n", alp);
printf("Number of Digits in the string is : %d\n", digit); printf("Number
of Special characters in the string is : %d\n\n", splch);
Sort the names in an alphabetical order by
using the string functions
#define ITEMS 5
#define MAXCHAR 20 int main( )
{
char string[ITEMS][MAXCHAR], dummy[MAXCHAR]; int i = 0, j = 0;
/* Reading the array*/
printf ("Enter names of %d items ",ITEMS); while (i < ITEMS)
scanf ("%s", string[i++]);
/* Sorting begins */
for (i=1; i < ITEMS; i++){
for (j=1; j <= ITEMS-i ; j++){
if (strcmp (string[j-1], string[j]) > 0){ /* Exchange of contents */
strcpy (dummy, string[j-1]);
strcpy (string[j-1], string[j]);
strcpy (string[j], dummy );
}
}
}
printf ("Alphabetical list");
for (i=0; i < ITEMS ; i++)
printf ("%s", string[i]);
}
OUTPUT
Enter names of 5 items
computers
architecture
organization
microprocessor
networking
Alphabetical list
architecture
computers
microprocessor
networking
organization