Exp 7
Exp 7
#include<stdio.h>
void main() {
char line[100];
int i, vowels = 0, consonants = 0, digits = 0, spaces = 0;
printf("Enter a line of string : ");
gets(line);
for (i = 0; line[i]!='\0'; i++) {
if (line[i] == 'a' || line[i] == 'e' || line[i] == 'i' || line[i] == 'o' || line[i] == 'u' || line[i] ==
'A' || line[i] == 'E' || line[i] == 'I' || line[i] == 'O' || line[i] == 'U') {
++vowels;
} else if ((line[i] >= 'a' && line[i] <= 'z') || (line[i] >= 'A' && line[i] <= 'Z')) {
++consonants;
} else if (line[i] >= '0' && line[i] <= '9') {
++digits;
} else if (line[i] == ' ') {
++spaces;
}
}
printf("Vowels = %d\n", vowels);
printf("Consonants = %d\n",consonants);
printf("Digits = %d\n",digits);
printf("White spaces = %d\n", spaces);
}
Write a program to remove duplicates character from a given string. Follow the given instructions
while writing the program.
#include <stdio.h>
#include<stdlib.h>
#include<string.h>
char removeDuplicate(char str[], int n) {
int index = 0,i,j;
for (i=0; i<n; i++) {
for (j = 0; j < i; j++)
if (str[i] == str[j])
break;
if (j == i)
str[index++] = str[i];
}
printf("%s",str);
}
int main() {
char str[200];
gets(str);
int n = sizeof(str) / sizeof(str[0]);
removeDuplicate(str, n);
return 0;
}
Find the most occurring character in a string.
#include<stdio.h>
#include<string.h>
int main() {
char s[100];
int i,max=0,stat[26]={0},ind;
scanf("%s",s);
for(i = 0; s[i] != '\0'; i++) {
ind = s[i]-97;
stat[ind]++;
}
for(i = 0; i < 26; i++) {
if(stat[i] > max) {
max = stat[i];
ind = i;
}
}
printf("%c",ind+97);
return 0;
}
The function string Reverse should take an input string ( Multiple words can be separated with
spaces ) and return the reversed string.
Write a program to reverse a String Using Recursion. Follow the instructions while writing the
program.
#include <stdio.h>
void reverseSentence();
int main() {
reverseSentence();
return 0;
}
void reverseSentence() {
char c;
scanf("%c", &c);
if( c != '\n') {
reverseSentence();
printf("%c",c);
}
}
Write a program to check whether the given string is a palindrome or not.
#include<stdio.h>
void main() {
char ch[80];
int i, j, length, flag = 0;
printf("String: ");
scanf("%s", ch);
length = 0;
while (ch[length] != '\0') {
length++;
}
for (i = 0, j = length - 1; i <= length / 2; i++, j--) {
if (ch[i] != ch[j]) {
flag++;
break;
}
}
if (flag == 0) {
printf("Palindrome\n", ch);
} else {
printf("Not a palindrome\n", ch);
}
}
Given a string, your task is to calculate the number of different strings that can be created using its
characters.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX_CHAR 26
int factorial(int n)
int fact = 1;
fact = fact * i;
return fact;
int freq[MAX_CHAR];
memset(freq, 0, sizeof(freq));
freq[str[i] - 'a']++;
int fact = 1;
}
// Driver code
int main()
char str[100];
printf("enter string:");
scanf("%s",&str);
printf("%d", countDistinctPermutations(str));
return 0;
}
Write a program to check whether the given two strings are equal or not without using string library
functions.
#include<stdio.h>
void main() {
char a[20], b[20];
int i = 0, flag = 0;
printf("string1 : ");
scanf("%s", a);
printf("string2 : ");
scanf("%s", b);
while (a[i] != '\0' && b[i] != '\0') {
if (a[i] != b[i]) {
flag = 1;
break;
}
i++;
}
if (flag == 0 && a[i] == '\0' && b[i] == '\0') {
printf("equal\n");
} else {
printf("not equal\n");
}
}