C Programs Strings Module 2
C Programs Strings Module 2
STRINGS
Output
Enter the String:qwerty
Length of string is 6
--------------------------------------------------------------------------------------------------
3) STRING COPY OPERATION USING strcpy()
/* string copy using strcpy()*/
#include<stdio.h>
#include<string.h>
void main()
{
char str1[25],str2[25];
printf("\nEnter the String1:");
scanf("%s",str1);
strcpy(str2,str1);
printf("Original String is, str1= %s\n",str1);
printf("Copied String is, str2=%s\n",str2);
}
4) STRING COPY OPERATION WITHOUT USING LIBRARY
FUNCTIONS
/* string copy without using strcpy()*/
#include<stdio.h>
#include<string.h>
void main()
{
char str1[25],str2[25];
int i;
printf("\nEnter the String1:");
scanf("%s",str1);
for(i=0; str1[i]!='\0';i++)
{
str2[i]=str1[i];
}
str2[i]='\0';
printf("Original String is, str1= %s\n",str1);
printf("Copied String is, str2=%s\n",str2);
}
Output
Enter the String1:welcome
Original String is, str1= welcome
Copied String is, str2=welcome
------------------------------------------------------------------------------------------------------
5) STRING CONCATENATION USING strcat()
/* string concatenation without using library functions*/
#include<stdio.h>
#include<string.h>
void main()
{
char str1[25],str2[25];
int i=0,j=0;
printf("\nEnter First String:");
scanf("%s",str1);
printf("\nEnter Second String:");
scanf("%s",str2);
strcat(str1,str2);
printf("\nConcatenated String is %s",str1);
}
6) STRING CONCATENATION WITHOUT USING LIBRARY
FUNCTIONS
/* string concatenation without using library functions*/
#include<stdio.h>
#include<string.h>
void main()
{
char str1[25],str2[25];
int i=0,j=0;
printf("\nEnter First String:");
scanf("%s",str1);
printf("\nEnter Second String:");
scanf("%s",str2);
while(str1[i]!='\0')
i++;
while(str2[j]!='\0')
{
str1[i]=str2[j];
j++;
i++;
}
str1[i]='\0';
printf("\nConcatenated String is %s",str1);
}
Output
Enter First String:aaa
Enter Second String:bbb
Concatenated String is aaabbb
-------------------------------------------------------------------------------------------------------
7) STRING COMPARISON USING strcmp()
/* string compare using strcmp()*/
#include<stdio.h>
#include<string.h>
void main()
{
char str1[25],str2[25];
int i=0,j=0;
printf("\nEnter First String:");
scanf("%s",str1);
printf("\nEnter Second String:");
scanf("%s",str2);
int val=strcmp(str1,str2);
if(val==0)
printf("\nStrings are equal");
else if(val<0)
printf("%s is less than %s",str1,str2);
else
printf("%s is greater than %s",str1,str2);
}
Output 1
Enter the string
malayalam
malayalam is a palindrome
Output 2
Enter the string
hello
hello is not a palindrome
—------------------------------------------------------------------------------------------------------------------
else
// Else increment the count of consonants
consonants++;
}
// Print the total count of vowels and consonants
printf("\nVowels: %d", vowels);
printf("\nConsonants: %d", consonants);
printf("\nSpaces: %d", spaces);
}
Output
Enter the string
hi how are you
Vowels: 6
Consonants: 5
Spaces: 3
—------------------------------------------------------------------------------------------------------------------
11) COUNT UPPERCASE AND LOWERCASE CHARACTERS IN A
STRING
// program to print no.of uppercase and lowercase letters in a string
#include <stdio.h>
void main() {
char str[100], chr;
int upper=0,lower=0;
printf("Enter a string: ");
scanf("%s",str);
for (int i = 0; str[i] != '\0'; ++i)
{
if (str[i] >= 'A' && str[i] <= 'Z')
upper++;
else if (str[i] >= 'a' && str[i] <= 'z')
lower++;
}
printf("No.of uppercase Characters = %d\n", upper);
printf("No.of lowercase Characters = %d\n", lower);
}
Output
Enter a string: WelcoME
No.of uppercase Characters = 3
No.of lowercase Characters = 4
—---------------------------------------------------------------------------------------------------
12) REVERSE A STRING WITHOUT USING STRING HANDLING
FUNCTIONS
// program to reverse a string
#include <stdio.h>
void main() {
char str[100], chr;
int length,i;
printf("Enter a string: ");
scanf("%s",str);
for (length = 0; str[length] != '\0'; length++);
printf("The reverse of the string is : ");
for(i=length-1;i>=0;i--)
{
printf("%c",str[i]);
}
}
Output
Enter a string: CAT
The reverse of the string is : TAC