0% found this document useful (0 votes)
35 views12 pages

Exp 7

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

Exp 7

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

Write a 

C program to convert a string into a long integer.


#include <stdio.h>
#include <stdlib.h>
int main(){
char inputString[200];
long asLong;
printf("Enter a number as string : ");
scanf("%s", inputString);
asLong = atol(inputString);
printf("After converting into long int : %ld\n", asLong);
return 0;
}
Write a program given a string S, change the smallest number of letters in S such that all adjacent
characters are different. Print the resultant string.
#include <stdio.h>
#include <string.h>
void main() {
char ch[100];
printf("Enter a string : ");
scanf("%s", &ch);
int n = strlen(ch);
for(int i = 1; i < n; i++) {
if(ch[i] == ch[i-1]) {
ch[i] = 'a';
while((ch[i] == ch[i - 1] || (i + 1 < n && ch[i] == ch[i + 1])))
ch[i]++;
i++;
}
}
printf("%s\n", ch);
}
Write a program to count the number of vowels, consonants, digits and spaces are presented in a given
string.

#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;

for (int i = 2; i <= n; i++)

fact = fact * i;

return fact;

int countDistinctPermutations(char str[100])

int length = strlen(str);

int freq[MAX_CHAR];

memset(freq, 0, sizeof(freq));

for (int i = 0; i < length; i++)

if (str[i] >= 'a')

freq[str[i] - 'a']++;

int fact = 1;

for (int i = 0; i < MAX_CHAR; i++)

fact = fact * factorial(freq[i]);

return factorial(length) / fact;

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

You might also like