Module 4 Strings
Module 4 Strings
Module 4 Strings
STRINGS
• String is a NULL character ('\0') terminated sequence of characters enclosed inside
double quotes.
• ‘\0’ is also called as string delimiter.
Length of a string
• Number of characters stored in string till the delimiter \0,exclude \0 (NULL character).
VCET,Puttur Page 1
22POP13/23 Module 4
---------------------------------------------------------------------------------------------------------------
Explain how strings are declared and initialized. (June 2017/Jan 20)
Declaring String Variable
Syntax:
char stringname[size];
• Size of the string should always be number of characters plus 1 because string should end
with '\0'.
Example: char name[20]; where name can store up to 20 characters.
Syntax:
char stringname[number of strings][number of characters];
Example: char name[5][20]; where name can store up to 5 strings and each string can have 20
characters.
Initialization of strings
• Storing characters in character array when they are declared.
Ex:
1: char str[6] = {‘I’,‘N’,‘D’,‘I’,‘A’,‘\0’};
2: char str[ ] = {‘I’,‘N’,‘D’,‘I’,‘A’,‘\0’};
3: char str[6] = “INDIA”;
In above example 3, we are declaring an array that has five characters, namely, I, N, D, I, and A.
Apart from these characters, a null character ('\0') is stored at the end of the string. So, the
internal representation of the string becomes INDIA '\0'. To store a string of length 5, we need 5
+ 1 locations (1 extra for the null character).
• Storing strings in two dimensions of character array when they are declared.
VCET,Puttur Page 2
22POP13/23 Module 4
-------------------------------------------------------------------------------------------------------------
Explain I/O functions for strings.(Jan 21)
READING A STRING
If we declare a string by writing char str[100]; Then str can be read in following ways:
a) scanf()
Syntax:
scanf(“control string”,argument1,argument2,….);
b) gets()
Reads a string from keyboard including white space. The string inputted using gets() is
automatically terminated with a null character.
Syntax:
gets(stringname);
Example:
char str[20];
………
gets(str);
c) getchar(),getch() or getche()
Syntax: charactername=getchar();
Strings can also be read by calling the getchar() function repeatedly to read a sequence of single
characters (unless a terminating character is entered) and simultaneously storing it in a character
array as shown below.
VCET,Puttur Page 3
22POP13/23 Module 4
i=0;
ch = getchar();// Get a character
while(ch != '*')
{
str[i] = ch;
i++;
ch = getchar();
}
str[i] = '\0';// Terminate str with null character
PRINTING A STRING
a) printf()
• Strings can be displayed using printf() by writing
printf("%s", str);
• We use the format specifier %s to output a string.
• We may also use width and precision specifications along with %s.
For example,
printf ("%5.3s", str);
The above statement would print only the first three characters in a total field of five
characters.
b)puts()
Syntax:
puts(stringname);
• This is unformatted output statement to display a string.
• Control string is not used.
Example:
char str[20];
………
puts(str);
Example Program:
#include <stdio.h>
void main()
{
char s1[100];
printf("Enter string\n");
gets(s1);
puts(s1);
}
Output:
Enter string
Hello
Hello
c) putchar() Strings can also be displayed by calling the putchar() function repeatedly to print a
sequence of single characters as shown below.
VCET,Puttur Page 4
22POP13/23 Module 4
i=0;
while(str[i] != '\0')
{
putchar(str[i]);// Print the character on the screen
i++;
}
------------------------------------------------------------------------------------------------------------
VCET,Puttur Page 5
22POP13/23 Module 4
• Syntax
strlen(stringname);
• Example:
strlen(“INDIA”);
Above statement returns 5
#include <stdio.h>
#include<string.h>
void main()
{
char str[ ]=“Programming”;
printf("Length of the string is %d\n",strlen(str));
}
Output:
Length of the string is 11
• Example:
strrev(“INDIA”);
Above statement returns AIDNI
Example program using strrev()
#include <stdio.h>
#include<string.h>
void main()
{
char str[ ]= "programming";
printf("The reversed string is %d\n", strrev(str));
}
Output:
The reversed string is gnimmargorp
3. COPY A STRING
VCET,Puttur Page 6
22POP13/23 Module 4
Output
Enter first string Programming
Enter second string Problem
Copied Sting is = Problem
strncpy()
• This function allows us to copy first 'n' character from one string to another.
• Syntax
strcnpy(dest, source,n);
• Example
VCET,Puttur Page 7
22POP13/23 Module 4
s1=Go
Example program using strncpy()
#include <stdio.h>
#include<string.h>
include <stdio.h>
#include<string.h>
void main()
{
char s1[10],s2[10];
printf("Enter first string\n");
gets(s1);
printf("Enter second string\n");
gets(s2);
strncpy(s1,s2,4);
printf("Copied Sting is = %s",s1);
}
Output
Enter first string Programming
Enter second string Problem
Copied Sting is = Prob
4. CONACTENATE A STRING
• There are two concatenating functions strcat() and strncat().
strcat()
• defined in string.h and used to join two strings together.
• Syntax
strcat(dest, source);
• Example:
char s1[ ] = “good”;
char s2[ ] = “morning”;
strcat(s1,s2);
Above statement returns
s1=goodmorning
Example program using strcat()
#include <stdio.h>
#include<string.h>
void main()
{
char s1[10],s2[10];
printf("Enter first string\n");
gets(s1);
VCET,Puttur Page 8
22POP13/23 Module 4
• Example:
char s1[ ] = “good”;
char s2[ ] = “morning”;
strncat(s1,s2,4);
Above statement returns
s1=goodmorn
Example program using strncat()
#include <stdio.h>
#include<string.h>
void main()
{
char str1[100], str2[100];
printf("Enter first string\n");
gets(str1);
printf("Enter second string\n");
gets(str2);
strncat(str1, str2, 2);
printf("Concatenated string is %s\n", str1);
}
Output:
Enter first string
hai
Enter second string
hello
Concatenated string is haihe
VCET,Puttur Page 9
22POP13/23 Module 4
5. COMPARE STRINGS
• There are two comparing functions strcmp() and strncmp().
strcmp()
• The library function strcmp(s1, s2) which is defined in string.h compares string s1 with
s2.
• It returns one among the following three values by subtracting ASCII values of string s2
from string s1.
• Positive integer – When ASCII value of string s1 is greater than the ASCII value of string
s2.
• Negative integer – When ASCII value of string s1 is smaller than the ASCII value of
string s2.
• Zero – When S1 and S2 are equal
• Syntax
result=strcmp(s1, s2);
Example 1
char s1[ ] = “AB”
char s2[ ]= “AE”
strcmp(s1,s2)
Above statement returns -3 i.e., s1<s2
Example 2
char s1[ ] = “ZA”
char s2[ ]= “AF”
strcmp(s1,s2)
Above statement returns 25 i.e., s1>s2
Example 3
char s1[ ] = “AA”
char s2[ ]= “AA”
strcmp(s1,s2)
Above statement returns 0 i.e., s1=s2
VCET,Puttur Page 10
22POP13/23 Module 4
• Example:
char s1[ ] = “AACD”
char s2[ ]= “AA”
strncmp(s1,s2,2)
Above statement returns 0 i.e., s1=s2
strcmpi()
• The strcmpi() function is same as that of the strcmp() function but the only difference is
that strcmpi() function is not case sensitive and on the other hand strcmp() function is the
case sensitive.
• Syntax:
strcmpi(dest, source);
VCET,Puttur Page 11
22POP13/23 Module 4
• Example:
strcmpi(s1, s2);
7.CONVERTING A STRING
• There are two converting functions strupr() and strlwr().
[Note: These are only available in ANSI C (Turbo C/C++) and are not available in the
standard C-GCC compiler.]
strlwr()
• defined in string.h and returns given string in lowercase.
• Syntax
strlwr(stringname);
• Example:
strlwr(“INDIA”);
Above statement returns india
strupr()
• defined in string.h and returns given string in uppercase.
• Syntax
strupr(stringname);
VCET,Puttur Page 12
22POP13/23 Module 4
• Example:
strupr(“india”);
Above statement returns INDIA
Example program using strlwr():
#include <stdio.h>
#include<string.h>
void main()
{
char str[ ]=“PROGRAMMING”;
printf(“The converted string is %d\n",strlwr(str));
}
Output:The converted string is programming
Q) Write a program to reverse a given string without using built in function strrev().
#include <stdio.h>
#include<string.h>
void main()
{
char s1[100],s2[100];
int length,i,j=0;
printf("Enter string\n");
scanf("%s",s1);
length=strlen(s1);
for(i=length-1;i>=0;i--)
{
s2[j]=s1[i];
j++;
}
s2[j]='\0';
printf(“Reversed string = %s”, s2);
}
Output:
Enter string
CPS
Reversed string = SPC
Q) Write a program to reverse a string without using built in function strrev() and check
for palindrome. (June 2014)
#include <stdio.h>
#include<string.h>
void main()
VCET,Puttur Page 14
22POP13/23 Module 4
{
char s1[100],s2[100];
int length, result,i,j=0;
printf("Enter string\n");
scanf("%s",s1);
length=strlen(s1);
for(i=length-1;i>=0;i--)
{
s2[j]=s1[i];
j++;
}
s2[j]='\0';
printf("Reversed string is %s\n",s2);
result=strcmp(s1,s2);
if(result==0)
printf("Palindrome");
else
printf("Not Palindrome");
}
Output:
Enter string
wow
Reversed string is wow
Palindrome
--------------------------------------------------------------------------------------------------------------
Q)Write a program to copy one string to another without using built in function and count
the no.of characters.(July 19)
#include <stdio.h>
void main()
{
char s1[100], s2[100];
int i;
printf("Enter string\n");
scanf("%s",s1);
for (i = 0; s1[i] != '\0'; i++)
{
s2[i] = s1[i];
}
s2[i] = '\0';
printf("Copied string is %s\n", s2);
printf(“No.of copied characters are % d\ n”, i);
}
VCET,Puttur Page 15
22POP13/23 Module 4
Output:
Enter string
Hello
Copied string is Hello
No. of copied characters are 5
-------------------------------------------------------------------------------------
Write a program to concatenate two strings without using built in function strcat().
(Appeared in June 2017/June 2016)
#include <stdio.h>
#include<string.h>
void main()
{
char str1[100], str2[100];
int i, j;
printf("Enter first string\n");
scanf("%s", str1);
printf("Enter second string\n");
scanf("%s", str2);
for (i = 0; str1[i] != '\0'; i++);
for (j = 0; str2[j] != '\0'; j++, i++)
{
str1[i] = str2[j];
}
str1[i] = '\0';
printf("Concatenated string is %s\n", str1);
}
Output:
Enter first string
Good
Enter second string
Morning
Concatenated string is GoodMorning
-----------------------------------------------------------------------------------------------------------------
Write a program to compare strings without using built in function strcmp(). [appeared in
June 2018]
#include <stdio.h>
#include<string.h>
VCET,Puttur Page 16
22POP13/23 Module 4
#include<stdlib.h>
void main()
{
char str1[100], str2[100];
int i, length1, length2;
printf("Enter first string\n");
scanf("%s", str1);
printf("Enter second string\n");
scanf("%s", str2);
length1 = strlen(str1);
length2 = strlen(str2);
if (length1 != length2)
printf("Strings are different\n");
else
{
for (i = 0; str1[i] != '\0'; i++)
{
if (str1[i] != str2[i])
{
printf("Strings are different\n");
exit(0);
}
}
printf("Strings are equal\n");
}
}
Output:
----------------------------------------------------------------------------------------------------------------
Arithmetic Operations on Characters:
C allows us to manipulate characters the same way we do with numbers.
ASCII value of a…z is 97 to 122.
ASCII value of A…Z is 65 to 91.
Egi):char x=’a’;
printf(“%d”,x); //will print 97 because ASCII value of a is 97.
ii)x=’z’-1; //It will assign 121 to x because ASCII value of z is 122
[Note:
VCET,Puttur Page 17
22POP13/23 Module 4
• The library functions toupper() and tolower() which are defined in ctype.h convert a
character into upper and lower case, respectively.
• The library functions isalpha() and isdigit() which are defined in ctype.h check whether
a character is alphabet and digit, respectively.
]
----------------------------------------------------------------------------------------------------------------
Q) Write a C program that read a sentence and print frequency of vowels and total count
of consonants.
#include<stdio.h>
#include<string.h>
#include<ctype.h>
void main() {
char str[100];
int i = 0, va = 0, ve = 0, vi = 0, vo = 0, vu = 0, c = 0;
printf("Enter any string\n");
gets(str);
for (i = 0; str[i] != '\0'; i++)
{
if (isalpha(str[i]))
{
switch (tolower(str[i]))
{
case 'a':
va++;
break;
case 'e':
ve++;
break;
case 'i':
vi++;
break;
case 'o':
vo++;
break;
case 'u':
vu++;
break;
default:
c++;
VCET,Puttur Page 18
22POP13/23 Module 4
}
}
}
printf("a :%d\n", va);
printf("e :%d\n", ve);
printf("i :%d\n", vi);
printf("o :%d\n", vo);
printf("u :%d\n", vu);
printf("no. of consonants=%d\n", c);
}
Output:
Enter any string
VCET Puttur.
a :0
e :1
i :0
o :0
u :2
no. of consonants=7
------------------------------------------------------------------------------------------------------------------
Q)Write a Program to count number of vowels and consonants.(Jan 21)
#include<stdio.h>
void main()
{
char str[100];
int i=0, vowels=0,consonant=0;
printf("Enter any string\n");
gets(str);
while(str[i]!='\0')
{
if(str[i]=='a' || str[i]=='e' || str[i]=='i'|| str[i]=='o' || str[i]=='u' || str[i]=='A' || str[i]=='E' ||
str[i]=='I' ||str[i]=='O'|| str[i]=='U')
vowels++;
else
consonant++;
i++;
}
printf("No.of vowels is %d\n",vowels);
printf("No.of consonant is %d\n",consonant);
}
Output:
VCET,Puttur Page 19
22POP13/23 Module 4
Enter a string
Hello
No.of vowels is 2
No.of consonants is 3
//using switch-case
#include<stdio.h>
#include<string.h>
#include<ctype.h>
void main() {
char str[100];
int i = 0, vowels = 0, consonants = 0;
printf("Enter any string\n");
gets(str);
for (i = 0; str[i] != '\0'; i++)
{
if (isalpha(str[i]))
{
switch (tolower(str[i]))
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
vowels++;
break;
default:
consonants++;
}
}
}
printf("no. of vowels=%d\n", vowels);
printf("no. of consonants=%d\n", consonants);
}
Output:
Enter any string
C programming.
no. of vowels=3
VCET,Puttur Page 20
22POP13/23 Module 4
no. of consonants=9
-----------------------------------------------------------------------------------------------------------------
Q)Write a program to read two strings s1 and s2 and compare whether they are equal or
not.If they are not, join them together.Then copy the contents of s1 to s3. Print and find the
length of all the three strings.
#include<stdio.h>
#include<string.h>
void main() {
char s1[100], s2[100], s3[100];
int result, l1, l2, l3;
printf("Enter two strings\n");
scanf("%s%s", s1, s2);
result = strcmp(s1, s2);
if (result != 0)
{
printf("Strings are not equal\n");
strcat(s1, s2);
} else
printf("Strings are equal\n");
strcpy(s3, s1);
l1 = strlen(s1);
l2 = strlen(s2);
l3 = strlen(s3);
printf("Length of %s is %d\n", s1, l1);
printf("Length of %s is %d\n", s2, l2);
printf("Length of %s is %d\n", s3, l3);
}
Output:
VCET,Puttur Page 21