•
•
• String Declaration:
o
o Syntax of string declaration
char
■ char
■ string_name .
■ size:
• String Initialization :
o Assigning a string literal (with or without size):
char S[] = “Hello“ ;
char S[50] = “Hello”;
o Assigning character by character:
char S[50] = {’H’,‘e’,‘l’,‘l’,‘o’,‘\0’};
• String Initialization:
• Note:
o
null character ‘\0’
o
strcpy()
o
Read a string input from user:
• To input a string, can use these functions:
• Function scanf() :
o scanf() function is used to read a non-space string. The scanf() function reads the
sequence of characters until it encounters whitespace (space, newline, tab, etc.).
char S[10];
scanf(“%s”, S);
• Notice:
• The S in scanf() already points to the address of the first element in the string, which is
why we don't need to use &.
• The number of characters cannot be greater than the size of string.
Read a string input from user:
• To input a string, can use these functions:
• Function gets() :
o scanf() function is used to read a string with space. The gets() function reads the
sequence of characters until it encounters enter key.
char S[10];
gets(S);
• Notice:
• The number of characters cannot be greater than the size of string.
Read a string input from user:
• To input a string, can use these functions:
• Function fgets() :
o fgets() function is used to read a limited string with space. The fgets() function reads
the sequence of characters until it encounters enter key.
char S[10];
fgets(S, 10, stdin);
• S – the name of string.
• 10 – the maximun number of input characters.
• stdin – It is the filehandle, from where the string is to be read.
Display a string:
• To display a string to screen, use the following functions:
• Function printf() :
o Function printf() prints the strings in the same line.
printf(S);
printf(“string: %s”, S);
• Function puts() :
o puts() prints the strings in the different lines (each string in one line).
puts(S);
String Elements:
• Since strings are actually arrays in C, you can access a string by referring to its index
number inside square brackets [].
■ char S[50] = “Hello“ ;
■ S[0] = ‘h‘;
• The element in the string is distinguished through position, with the first element has
index 0, the second has index 1 and so on.
• Please notice that the element of string is a character, so we must use the single quote
(‘ ‘) when assigning new value for this element.
• String Traversal:
• Similar to iterating over arrays, in C/C++, a for loop is used when iterating over a string of
characters.
• String traversal using for loop:
for (int i = 0; i < strlen(S); i++)
{
string_name[i];
}
• Notice :
• strlen(S): function return the length of string (the number of characters).
• string.h C library: The C language comes bundled with “string.h” which contains some
useful string-handling functions. Some of them are as follows
Function Name Description
strlen(string_name) Returns the length of string name.
strcpy(s1, s2) Copies the contents of string s2 to string s1.
strcmp(s1, s2) Compares the first string with the second string. If strings are the same it returns 0.
strcat(s1, s2) Concat s1 string with s2 string and the result is stored in the first string.
strlwr() Converts string to lowercase.
strupr() Converts string to uppercase.
// string function
#include <stdio.h>
#include <string.h>
int main()
{
char str[50];
printf(“Input string: ");
fgets(str, 50, stdin);
int n = strlen(str);
strupr(str);
printf("\nUppercase string: %s ",str);
return 0;
}
• Removing characters:
• Removing Characters from a String: Removing specific characters (e.g., spaces) from a
string can be done by shifting characters left in the array.
• Features of removing algorithm:
■ Finding the index of character which is removed.
■ Shift characters left in the array starting at the finding index.
// string function
#include <stdio.h>
#include <string.h>
int main()
{
char S[50];
printf("Input string: ");
fgets(S, 50, stdin);
// Delete spaces in string
for(int i = 0;i<strlen(S);i++)
{
if(S[i] == ' ')
{
for(int j = i;j<strlen(S);j++)
S[j] = S[j+1];
}
}
printf("\nString after deleting: %s ",S);
return 0;
}
• Inserting characters:
• Insert characters to a String: To insert characters
into a string, need to shift elements in the
character array and then place the new
characters into the desired position.
• Features of removing algorithm:
■ Finding the desired position.
■ Shift characters right in the array starting at
the position.
■ Place the new characters into the desired
position.
// string function
#include <stdio.h>
#include <string.h>
int main()
{
char S[50];
printf("Input string: ");
fgets(S, 50, stdin);
// Insert 1 space after . character
for(int i = 0;i<strlen(S);i++)
{
if(S[i] == '.')
{
for(int j = strlen(S);j>=i+1;j--) S[j+1] = S[j];
S[i+1] == ' ';
}
}
printf("\nString after inserting: %s ",S);
return 0;
}
• Pattern Searching:
• Naive Pattern Searching algorithm:
• Naive pattern searching is the simplest method among other pattern-searching
algorithms.
• It checks for all characters of the main string to the pattern. This algorithm is
helpful for smaller texts. It does not need any pre-processing phases. We can find
the substring by checking once for the string.