0% found this document useful (0 votes)
4 views9 pages

C Programs Strings Module 2

The document provides various C programming examples related to string manipulation, including functions for calculating string length, copying, concatenating, comparing, checking for palindromes, and counting characters. Each example includes code snippets demonstrating both the use of standard library functions and custom implementations without them. Additionally, it covers counting vowels, consonants, uppercase, and lowercase characters, as well as reversing a string.

Uploaded by

sarangavethan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views9 pages

C Programs Strings Module 2

The document provides various C programming examples related to string manipulation, including functions for calculating string length, copying, concatenating, comparing, checking for palindromes, and counting characters. Each example includes code snippets demonstrating both the use of standard library functions and custom implementations without them. Additionally, it covers counting vowels, consonants, uppercase, and lowercase characters, as well as reversing a string.

Uploaded by

sarangavethan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

PROGRAMS – MODULE 2

STRINGS

1) STRING LENGTH USING strlen()


/* string length using strlen()*/
#include<stdio.h>
#include<string.h>
void main()
{
char str1[25];
printf("\nEnter the String:");
scanf("%s",str1);
printf("\nLength of string is %ld",strlen(str1));
}
2) STRING LENGTH WITHOUT USING LIBRARY FUNCTION
a) using for loop
/* string length without using library function*/
#include<stdio.h>
#include<string.h>
void main()
{
char str1[25];
int len;
printf("\nEnter the String:");
scanf("%s",str1);
for(len=0;str1[len]!='\0'; len++); // semicolon is needed
printf("\nLength of string is %d",len);
}

b) using while loop


/* string length without using library function*/
#include<stdio.h>
#include<string.h>
void main()
{
char str1[25];
int len=0;
printf("\nEnter the String:");
scanf("%s",str1);
while(str1[len]!='\0')
​ len++;
printf("\nLength of string is %d",len);
}

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);
}

8) STRING COMPARISON WITHOUT USING LIBRARY FUNCTIONS


/* string compare without using strcmp()*/
#include<stdio.h>
#include<string.h>
void main()
{
char str1[25],str2[25];
int len1=0,len2=0,i;
printf("\nEnter First String:");
scanf("%s",str1);
printf("\nEnter Second String:");
scanf("%s",str2);
while(str1[len1] != '\0')
​ len1 ++;
while(str2[len2] != '\0')
​ len2 ++;
i = 0;
while(str1[i]== str2[i] && str1[i] !='\0')
{
​ i++;
}
if(str1[i] > str2[i])
printf ("%s is greater than %s",str1,str2);
else if (str1[i] < str2[i])
printf("%s is less than %s",str1,str2);
else
​ printf ("Strings are equal\n");
}
Output 1
Enter First String:cat
Enter Second String:cat
Strings are equal
Output 2
Enter First String:cat
Enter Second String:mat
cat is less than mat
Output 3
Enter First String:mat
Enter Second String:cat
mat is greater than cat
9) STRING PALINDROME
/* string palindrome*/
#include<stdio.h>
#include<string.h>
void main()
{
char str[20];
int i, length, flag=0;
printf("Enter the string\n");
scanf("%s", str);
length=strlen(str);
//for(length=0;str[length]!='\0';length++);
for (i = 0; i < length / 2; i++)
{
if (str[i] != str[length - i - 1])
{
flag = 1;
break;
}
}
if (flag)

printf("%s is not a palindrome\n", str);


else
printf("%s is a palindrome\n", str);
}

Output 1
Enter the string
malayalam
malayalam is a palindrome
Output 2
Enter the string
hello
hello is not a palindrome
—------------------------------------------------------------------------------------------------------------------

10) COUNT THE NO.OF VOWELS, CONSONANTS AND SPACES IN A


STRING
/* count the number of vowels,consonants and spaces*/
#include <stdio.h>
#include<string.h>
int main()
{
char ch,str[20];
int i,vowels = 0, consonants = 0, spaces=0;
printf("Enter the string\n");
//scanf("%[^\n]s",str);
gets(str);

// Take each character from this string to check


for (i = 0; str[i] != '\0'; i++)
{
ch = str[i];
// If this character is a vowel,
// increment the count of vowels
if (ch == 'a' || ch == 'e'
|| ch == 'i' || ch == 'o'
|| ch == 'u' || ch == 'A'
|| ch == 'E' || ch == 'I'
|| ch == 'O' || ch == 'U')
vowels++;

// If this character is a space


// increment the count of spaces
else if (ch == ' ')
spaces++;

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

You might also like