E-Note 30035 Content Document 20241231023043PM
E-Note 30035 Content Document 20241231023043PM
1
C Strings
In C language a string is group of characters (or) array of characters, which is terminated by
delimiter \0 (null). Thus, C uses variable-length delimited strings in programs.
Declaring Strings:-
C does not support string as a data type. It allows us to represent strings as character
arrays. In C, a string variable is any valid C variable name and is always declared as an array
of characters.
3
Initializing location character by character
• Consider the following declaration With initialization:
char b[9]= { ‘C’, ‘O’,’M’,’P’,’U’,’T’,’E’,’R’};
C O M P U T E R \0
0 1 2 3 4 5 6 7 8
4
Partial array initialization
• Consider the following declaration With initialization:
char a[10]= {‘R’,’A’,’M’,’A’};
R A M A \0 \0 \0 \0 \0 \0
0 1 2 3 4 5 6 7 8 9
5
Initialization without specifying the size
• Consider the following declaration With initialization:
char a[]= {‘D’,’S’,’A’,’T’,’M’};
D S A T M
0 1 2 3 4
Note: sizeof(b) will be 8 . Note that ‘\0’ is not inserted at the end of the
string.
6
Array initialization with a string
• Consider the following declaration With string initialization:
char b[]=“COMPUTER”;
C O M P U T E R \0
0 1 2 3 4 5 6 7 8
7
Storing strings in memory:-
• In C a string is stored in an array of characters and terminated by \0
(null).
8
• Because strings are variable-length structure, we must provide
enough room for maximum- length string to store and one byte for
delimiter.
String Array
Why do we need null?
• A string is not a datatype but a data structure. String implementation
is logical not physical. The physical structure is array in which the
string is stored. The string is variable-length, so we need to identify
logical end of data in that physical structure.
9
STRING I/O FUNCTIONS
10
FORMATTED I / O FUNCTIONS
Formatted Input Function:
The input function scanf ( ) is used to read the string. The
conversion code used is %S to read a string of characters.
Example: char addr[15];
scanf (“%s”, addr);
The “&” operator should not be used to read the string variables.
Formatted Output Function:
The output function printf( ) is used to display/print the string.
The conversion code/ format specifier used is %s to print the
string of characters.
Example: char a[10]; scanf (“%s”, a);
printf (“%s”, a);
Prints the characters ie., group of characters on to the monitor.
11
Reading strings from terminal
formatted input function:- scanf can be used with %s format specification to
read a string.
Here don't use “&‟ because name of string is a pointer to array. The problem
with scanf is that it terminates its input on the first white space it finds.
Ex:- NEW YORK
12
Writing strings on to the screen:-
Using formatted output functions:- printf with %s format specifier we
can print strings in different formats on to screen.
Ex:- char name[10];
printf(“%s”,name);
13
UNFORMATTED I / O
gets( ): (unformatted input function)
To read sequence of characters from keyboard with spaces in between and store them in memory
locations. gets( ) function is used.
Syntax:
gets(str);
str - string variable
Reads string of characters from keyboard till users presses “Enter key”.
char s1[10]=“DSATM”;
Char s2[]=“DSI” s1[0] s1[1] s1[2] s1[3] s1[4] s1[5]s1 [6] s1[7] s1[8] s1 [9]
D S A T M \0 \0 \0 \0 \0
printf(“%d”, sizeof(s2)); //
printf(“%d”, strlen(s2)); //
23
strcat (str1, str2) - append string str2 to str1.
This function is used to concatenate two strings. i.e., it appends one string at
the end of the specified string. Its syntax as follows:
strcat(string1,string2);
where string1 and string2 are one-dimensional character arrays.
This function joins two strings together. In other words, it adds the string2 to
string1 and the string1 contains the final concatenated string.
E.g., string1 contains prog and string2 contains ram, then string1 holds
program after execution of the strcat() function
Example:
char str1[10 ] = “VERY”;
char str2[ 5] =”GOOD”;
strcat(str1,str2);
24
str1[0] str1[1] str1[2] str1[3] str1[4] str1[5] str1 [6] str1[7] str1[8] str1 [9]
V E R Y \0 \0 \0 \0 \0 \0
strcat (str1,str2);
str1[0] str1[1] str1[2] str1[3] str1[4] str1[5] str1 [6] str1[7] str1[8] str1 [9]
V E R Y G O O D \0 \0
25
A program to concatenate one string with another
using strcat() function
#include<stdio.h>
#include<string.h>
int main()
{
char s1[30],s2[15];
printf(“\n Enter first string:”);
gets(s1);
printf(“\n Enter second string:”);
gets(s2);
if(sizeof(s1)>strlen(s1)+srtlen(s2))
{
strcat(string1,string2);
printf(“\n Concatenated string=%s”,string1);
}
else
printf(“Concatenation not possible\n)
} 26
USER DEFINED FUNCTION
void concatenate(char s1[], char s2[])
{ int main()
int i, j; {
i = 0; char s1[30],s2[15];
while (s1[i] != '\0') printf(“\n Enter first string:”);
gets(s1);
{
printf(“\n Enter second string:”);
i++; gets(s2);
} if(sizeof(s1)>strlen(s1)+srtlen(s2))
j = 0; {
while (s2[j] != '\0') concatenate(string1,string2);
printf(“\n Concatenated string=%s”,string1);
{
}
s1[i] = s2[j]; else
j++; printf(“Concatenation not possible\n)
i++; }
}
s1[i] = '\0';
}
27
strncat(char s1[],char s2[],int n)
#include<stdio.h>
#include<string.h>
int main()
{
char s1[30],s2[15];
printf(“\n Enter first string:”);
gets(s1);
printf(“\n Enter second string:”);
gets(s2);
if(sizeof(s1)>strlen(s1)+srtlen(s2))
{
strncat(string1,string2,3);
printf(“\n Concatenated string=%s”,string1);
}
else
printf(“Concatenation not possible\n)
}
28
str1[0] str1[1] str1[2] str1[3] str1[4] str1[5] str1 [6] str1[7] str1[8] str1 [9]
V E R Y \0 \0 \0 \0 \0 \0
strncat (str1,str2,3);
str1[0] str1[1] str1[2] str1[3] str1[4] str1[5] str1 [6] str1[7] str1[8] str1 [9]
V E R Y G O O \0 \0 \0
29
strcmp() compare two strings str1 & str2.
The strcmp() function compares two strings and returns 0 if both strings are identical
Present in string.h library
Syntax:
int strcmp (const char* str1, const char* str2);
30
Return Value from strcmp()
ASCII
A=65 a=97
B=66 b=98
Return Value Remarks C=67 c=99
.
0 if both strings are identical (equal)
.
if the ASCII value of the first .
negative unmatched character is less than .
second.
31
ASCII
Example: C strcmp() function A=65 a=97
B=66 b=98
C=67 c=99
#include <stdio.h>
str1 str2 str3
#include <string.h>
a a a
int main() str1[0] str2[0] str3[0]
{ char str1[] = "abcd", str2[] = "abCd", str3[] = "abcd"; b b
str1[1] str2[1] b str3[1]
int result1,result2;
c c
str1[2] str2[2] C str3[2]
// comparing strings str1 and str2
result1 = strcmp(str1, str2); d str2[3] d str3[3] d
str1[3]
printf("strcmp(str1, str2) = %d\n", result1);
\0 \0
str1[4] str2[4] \0 str3[4]
// comparing strings str1 and str3
result2 = strcmp(str1, str3);
printf("strcmp(str1, str3) = %d\n", result2); result1=str1[2]-srt2[2] result2=str1[4]-srt3[4]
=99-67 =\0 - \0
}
=32 =0-0
=0
32
• Output
ASCII
strcmp(str1, str2) = 32 A=65 a=97
B=66 b=98
strcmp(str1, str3) = 0 C=67 c=99
33
ASCII
A=65 a=97
B=66 b=98
C=67 c=99
34
User defined function for strcmp()
#include <stdio.h>
#include <stdio.h> str1 str2
int my_strcmp(char s1[],char s2[]);
int my_strcmp(char s1[],char s2[]);
{ int=0; d d
int main() str1[0] str2[0]
{ int difference; while (s1[i] == s2[i]) s
str1[1] str2[1] s
char s1[100], s2[100]; {
printf("Input a string1\n"); if (s1[i] == '\0' || s2[i] == '\0') i
str1[2] str2[2] i
gets(s1); break;
printf("Input a string2\n"); i++; \0 str2[3] \0
str1[3]
gets(s2); }
difference=my_strcmp(s1,s2); return s1[i]-s2[i]; str1[4] str2[4]
If(difference==0)
}
printf(“%s==%s”,s1,s2);
else if (difference>0)
printf(“%s>%s”,s1,s2); Output1
else dsi==dsi
printf(“%s<%s”,s1,s2);
} 35
str1 str2 ASCII
#include <stdio.h>
d A=65 a=97
int my_strcmp(char s1[],char s2[]); str1[0] str2[0] d
B=66 b=98
{ int=0; C=67 c=99
s s
while (s1[i] == s2[i]) str1[1] str2[1]
{ i
str1[2] str2[2] i
if (s1[i] == '\0' || s2[i] == '\0')
break; \0 str2[3] a
str1[3]
i++;
} str1[4] str2[4] \0
return s1[i]-s2[i];
}
Output2 srt3[3]-str2[3]
dsi < dsia =\0-a
=0-97
=-97
36
Array of strings
37
Array of strings
We have array of integers, array of floating point numbers, etc.. similarly
we have array of strings also.
Collection of strings is represented using array of strings.
Declaration:-
char arr[row][col];
where,
arr - name of the array
row - represents number of strings
col - represents size of each string
38
Initialization:-
char arr[row][col] = {list of strings };
Example:-
char city[5][10] = { “DELHI”, 0 1 2 3 4 5 6 7 8 9
“CHENNAI”, 0 D E L H I \0
“BANGALORE”,
1 C H E N N A I \0
“HYDERABAD”,
“MUMBAI” }; 2 B A N G A L O R E \0
3 H Y D E R A B A D \0
4 M U M B A I \0
int main()
{
int i;
char city[5][10] = { "DELHI",
"CHENNAI",
"BANGALORE",
"HYDERABAD",
"MUMBAI" };
printf("strings are:\n");
for(i=0;i<=5;i++)
{
printf("%s\n", city[i]);
}
}
40
Write a C program to read and print array of strings.
#include <stdio.h>
int main()
{ 0 1 2 3 4 5 6 7 8 9. . .
int i,n;
char city[10][20]; 0
printf("Enter the number of string:\n");
scanf("%d", &n); 1
41
OUTPUT:
Enter the number of string:
5
enter the strings:
DSATM
DSCE
DSI
DSBA
DSU
strings are:
DSATM
DSCE
DSI
DSBA
DSU
42
PROGRAM-7
7 a) Write functions to implement string operations such as compare, concatenate, and find string length. Use the
parameter passing techniques.
}
int compare_strings(char s1[], char s2[])
void concatenate(char s1[], char s2[])
int string_length(char s1[])
{ {
{
int i = 0; int i, j;
int i = 0;
while (s1[i] == s2[i]) i = 0;
while (s1[i] != '\0') { while (s1[i] != '\0')
i++; if (s1[i] == '\0' || s2[i] == '\0') {
break;
return i; i++;
i++;
} }
}
if (s1[i] == '\0' && s2[i] == '\0') j = 0;
return 0; while (s2[j] != '\0')
else {
return 1; s1[i] = s2[j];
} j++;
i++;
}
s1[i] = '\0';
}
OUTPUT:
Input string1
dsatm
Input string2
dsi
Length of string1: 5
Length of string2: 3
Unequal string
String obtained on concatenation: dstamdsi
Write a C program that reads a sentence and
prints the frequency of each of the vowels
and total count of consonants
46
#include <stdio.h>
void main()
{
char str[80];
int i, vowels = 0, consonants = 0;
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
W E L C O M E T O D S A T M \0
printf("Enter a str \n");
gets(str);
48
To print frequency of each vowel and total
count of consonants in a sentence
49
#include<stdio.h>
void main()
{
char s[100];
printf("\nIn the sentence \"%s\" :",s);
int j, ac=0, ec=0, ic=0, oc=0, uc=0, cc=0;
printf("Enter a sentence\n");
gets(s); printf("\nThe frequency vowel of A/a is %d",ac);
for(j=0;s[j]!='\0';j++)
{ printf("\nThe frequency vowel of E/e is %d",ec);
if(isalpha(s[j]))
{ printf("\nThe frequency vowel of I/i is %d",ic);
if(s[j]=='A'||s[j]=='a')
printf("\nThe frequency vowel of O/o is %d",oc);
ac++;
else if(s[j]=='E'||s[j]=='e’)
ec++; printf("\nThe frequency vowel of U/u is %d",uc);
else if(s[j]=='I'||s[j]=='i’)
ic++; printf("\nThe total count of all consonents is %d",cc);
else if(s[j]=='O'||s[j]=='o’)
oc++; }
else if(s[j]=='U'||s[j]=='u’)
uc++;
else if(s[j]!=' ‘)
cc++;
}
}
50