Module 2-Part 2
Strings
Strings - Declaring a string variable,
Reading and displaying strings, String
related library functions – Programs
for string matching.
1
2
3
4
5
#include <stdio.h>
int main()
{
char str[20];
printf("Enter the String\n");
scanf("%s", str);
printf("String: %s\n", str);
return 0;
}
6
Syntax:
gets (S);
where ‘S’ is a string variable.
7
Syntax:
puts (S);
where ‘S’ is string variable.
8
9
In-built String Function : strlen()
Syntax
int strlen(str);
where, str indicates the string whose length needs to be determined.
• Find the length of a string excluding ‘\0’ .
10
12
13
14
Example for strcmp()
#include <stdio.h>
#include <string.h> // Required for strcmp()
int main()
{
char s1[20] = "Hello";
char s2[20] = "World";
if (strcmp(s1, s2) == 0)
printf("string 1 and string 2 are equal");
else
printf("string 1 and string 2 are different");
return 0;
}
Output
string 1 and string 2 are different 15
Example for strcmpi()
#include <stdio.h>
#include <string.h> // Required for strcmp()
int main()
{
char s1[20] = "Hello";
char s2[20] = “HeLLo";
if (strcmpi(s1, s2) == 0)
printf("string 1 and string 2 are equal");
else
printf("string 1 and string 2 are different");
return 0;
}
Output
string 1 and string 2 are equal 16
#include <stdio.h>
#include <string.h> // Required for string functions
int main()
{
char str[20] = "Hello World";
printf("Lower case string: %s\n", strlwr(str));
printf("Upper case string: %s\n", strupr(str));
return 0;
}
17
#include <stdio.h>
#include <string.h> // Required for string functions
int main()
{
char str[20] = "Hello World";
printf(“Reversed String is: %s\n", strrev(str));
return 0;
}
Reversed String is:dlroW olleH
18
19
20
2D String reading and printing
#include <stdio.h>
int main()
{
int i, n;
char str[10][20]; //Stores up to 10 strings, each with max length 19
printf("Enter number of strings: ");
scanf("%d", &n);
printf("Enter the strings:\n");
for (i = 0; i < n; i++)
{
scanf("%s", str[i]); // Reads a word (not a full sentence)
}
printf("\nThe strings are:\n");
for (i = 0; i < n; i++)
{
printf("%s\n", str[i]);
}
return 0; } 21
22
Character functions
• These functions are included in ctype.h
• They help in character classification and conversion.
• isalpha(c): Returns non-zero if c is an alphabet.
• isdigit(c): Returns non-zero if c is a digit.
• islower(c): Returns non-zero if c is a lowercase letter.
• isupper(c): Returns non-zero if c is an uppercase
letter.
• tolower(c): Converts c to lowercase.
• toupper(c): Converts c to uppercase. 23
#include <stdio.h>
#include <ctype.h> // for character functions
int main()
{
int i, n;
char s1 = 'a', s2 = 'B', s3 = '1', c;
if (isalpha(s1))
printf("%c is an alphabet\n", s1);
if (isdigit(s3))
printf("%c is a digit\n", s3);
if (islower(s1))
printf("%c is a lower case letter\n", s1);
24
if (isupper(s2))
printf("%c is an upper case letter\n", s2);
c = tolower(s2);
printf("Lower case of %c is %c\n", s2, c);
c = toupper(s1);
printf("Upper case of %c is %c\n", s1, c);
return 0;
}
25
Program to find length of a string without using strlen() function
#include<stdio.h>
int main()
{
char str[10];
int i,length=0;
printf("Enter a string: \n");
scanf("%s",str);
for(i=0; str[i]!='\0'; i++)
{
length++;
}
printf("Length of input string: %d",length);
return 0;
}
26
Program to copy the contents of one string to another, without using the strcpy() function
#include <stdio.h>
#include <string.h>
int main()
{
char s1[20],s2[20];
int len,i;
printf("Enter string:");
gets(s1);
len=strlen(s1);
for(i=0;i<=len;i++)
{
s2[i]=s1[i];
}
printf("The new string = %s",s2);
return 0;
}
Reverse a string without using library functions
#include <stdio.h>
int main()
{
char str[20],rev[20];
int i,j,len=0;
printf("Enter the string: ");
gets(str);
for(i=0;str[i]!=‘\0’;i++)
len++;
j=len-1;
for(i=0;i<len;i++)
{
rev[i]=str[j];
j--;
}
rev[i]='\0';
printf("Reverse=%s",rev);
return 0; }
Program to read a sentence and display count of white spaces
#include <stdio.h>
int main()
{
char str[50];
int i=0,space=0;
printf("Enter a string\n");
gets(str);
while(str[i]!=‘\0')
{
if(str[i]==' ')
{
space++;
}
i++;
}
printf("Total white spaces :%d ",space);
return 0; 29
//Implement string concatenation without using built in functions.
while(str2[j]!='\0')
#include<stdio.h> {
int main() str1[i]=str2[j];
{ j++;
char str1[25],str2[25]; i++;
int i=0,j=0; }
printf("\nEnter 1st String:"); str1[i]='\0';
gets(str1); printf("\nConcatenated String is
printf("Enter 2nd String:"); %s",str1);
gets(str2); return 0;
}
while(str1[i]!='\0')
i++;
30
//Implement string comaprison without using built in functions.
#include <stdio.h>
int main()
{
char s1[30]="Hello World";
int i,flag=0; if(flag==0)
char s2[30] = "Hello World"; printf(“Strings are equal");
for(i=0;s2[i]!='\0' || s1[i]!='\0';i++) else
{ printf(“Strings are not equal");
if(s1[i]!=s2[i]) return 0;
{ }
flag=1;
break;
}
}
31
Program to display the count of upper case and lowercase characters in a string
#include<stdio.h>
#include<ctype.h>
int main()
{
char str[20];
int i=0,upper=0,lower=0;
printf("Enter String:");
gets(str);
while(str[i]!='\0')
{
if(isupper(str[i]))
upper++;
else if(islower(str[i]))
lower++;
i++;
}
printf("\nCount of uppercase characters:%d",upper);
printf("\nCount of lowercase characters:%d",lower);
return 0;} 32
program to read names of n persons and display those
names in alphabetical order.
#include<stdio.h>
#include<string.h>
int main()
{
int n,i,j;
char names[30][30],temp[30];
printf("Enter the value of n:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the name:");
scanf("%s",names[i]);
}
33
for(i=0;i<n-1;i++)
{
for(j=0;j<n-i-1;j++)
{
if(strcmp(names[j],names[j+1])>0)
{
strcpy(temp,names[j]);
strcpy(names[j],names[j+1]);
strcpy(names[j+1],temp);
}
}
}
printf("Names in Ascending order\n");
for(i=0;i<n;i++)
{
printf("%s\n",names[i]);
}
return 0;
} 34
//Pattern matching
#include <stdio.h>
int main()
{
char text[100], pattern[100];
int i, j, textLength = 0, patternLength = 0, found = 0;
char ch;
printf("Enter the text: "); // Taking input for text manually
i = 0;
while ((ch = getchar()) != '\n') // Read until newline
{
text[i++] = ch;
}
text[i] = '\0'; // Null-terminate the string
// Calculating text length manually
while (text[textLength] != '\0’)
{
textLength++;
} 35
// Taking input for pattern manually
printf("Enter the pattern: ");
i = 0;
while ((ch = getchar()) != '\n') { // Read until newline
pattern[i++] = ch;
}
pattern[i] = '\0'; // Null-terminate the string
// Calculating pattern length manually
while (pattern[patternLength] != '\0') {
patternLength++;
}
36
printf("Pattern found at index: ");
for (i = 0; i <= textLength - patternLength; i++) {
for (j = 0; j < patternLength; j++) {
if (text[i + j] != pattern[j]) {
break; // If mismatch occurs, break the loop
}
}
if (j == patternLength) { // If pattern is found
printf("%d ", i);
found = 1;
}
}
if (!found) {
printf("Pattern not found.");
}
printf("\n");
return 0;
}
37