You are on page 1of 15

PPS-7

Name:- Aparannha Roy

Roll:- CSE/20/48

1. Write a program to check whether a given string is palindrome


or not. Also display the reverse of the given string.

= #include <stdio.h>
#include <string.h>

int main()
{
char str[100];
int i, len, flag;
flag = 0;

printf("\n Please Enter any String : ");


gets(str);

len = strlen(str);

for(i = 0; i < len; i++)


{
if(str[i] != str[len - i - 1])
{
flag = 1;
break;
}
}
if(flag == 0)
{
printf("\n %s is a Palindrome String", str);
}
else
{
printf("\n %s is Not a Palindrome String", str);
}

return 0;
}
Output:-

2. Write a program to count total number of characters in the given


string.

= #include<stdio.h>
#include<string.h>
int main()
{
char str[20];
int i=0,l;
printf("Enter the string:");
gets(str);
while(str[i]!='\0')
{
i++;
}
l=i;
printf("String length is %d",l);
return 0;
}
Output:-

3. Write a program to display the frequency of each character in


the given string.

= #include <stdio.h>
#include <string.h>

int main()
{
char s[1000];
int i,j,k,count=0,n;

printf("Enter the string : ");


gets(s);

for(j=0;s[j];j++);
n=j;

printf(" frequency count character in string:\n");

for(i=0;i<n;i++)
{
count=1;
if(s[i])
{

for(j=i+1;j<n;j++)
{

if(s[i]==s[j])
{
count++;
s[j]='\0';
}
}
printf(" '%c' = %d \n",s[i],count);

return 0;
}

Output:-

4. Write a program to count the vowels, consonents, spaces from


the given string.

= #include<stdio.h>
#include<string.h>
int main()
{
char st[20];
int i,l,v=0,sp=0,c=0;
printf("Enter the string");
gets(st);
l=strlen(st);
for(i=0;i<l;i++)
{

if(st[i]=='a'||st[i]=='e'||st[i]=='i'||st[i]=='o'||st[i]=='u'||st[i]=='A'||s
t[i]=='E'||st[i]=='I'||st[i]=='O'||st[i]=='U')
{
v++;
}
else if((st[i]>='a'&&st[i]<='z')||(st[i]>='A'&&st[i]<='Z'))
c++;
else
sp++;
}
printf("vowels=%d\nconsonant= %d\n spch= %d",v,c,sp);
return 0;
}

Output:-

5. Write a program to replace multiple spaces of a string by a


single space.

= #include <stdio.h>

int main()
{
char text[1000], blank[1000];
int c = 0, d = 0;
printf("Enter some text\n");
gets(text);

while (text[c] != '\0') {


if (text[c] == ' ') {
int temp = c + 1;
if (text[temp] != '\0') {
while (text[temp] == ' ' && text[temp] != '\0') {
if (text[temp] == ' ') {
c++;
}
temp++;
}
}
}
blank[d] = text[c];
c++;
d++;
}

blank[d] = '\0';

printf("Text after removing blanks\n%s\n", blank);

return 0;
}

Output:-
6. Write a program to count total number of a particular word in a
given string.

= #include <stdio.h>
int stringLength(char*);

int main()
{
char str[100]={0};
int length;

printf("Enter any string: ");


scanf("%s",str);
length=stringLength(str);
printf("String length is : %d\n",length);
return 0;
}

int stringLength(char* txt)


{
int i=0,count=0;

while(txt[i++]!='\0'){
count+=1;
}

return count;
}
Output:-
7. Write a program to count the length of a given string without
using strlen() function also count total number of characters of
that string.

= #include <stdio.h>
#include <string.h>

int main()
{
char s[200];
int count = 0, i;

printf("Enter the string:\n");


scanf("%[^\n]s", s);
for (i = 0;s[i] != '\0';i++)
{
if (s[i] == ' ' && s[i+1] != ' ')
count++;
}
printf("Number of words in given string are: %d\n", count +
1);
return 0;
}

Output:-
8. Write a program to merge 2nd string at the end of 1 st string
without using strcat() function.

= #include<stdio.h>
int main(int)
{
char str1[25],str2[25];
int i=0,j=0;
printf("\nEnter First String:");
gets(str1);
printf("\nEnter Second String:");
gets(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);
return 0;
}

Output:-
9. Write a program to copy a string into another string without
using strcpy() function.

=
#include <stdio.h>
#include <string.h>

int main()
{
char Str1[100], Str2[100];
int result, i;

printf("\n Please Enter the First String : ");


gets(Str1);

printf("\n Please Enter the Second String : ");


gets(Str2);

for(i = 0; Str1[i] == Str2[i] && Str1[i] == '\0'; i++);

if(Str1[i] < Str2[i])


{
printf("\n str1 is Less than str2");
}
else if(Str1[i] > Str2[i])
{
printf("\n str2 is Less than str1");
}
else
{
printf("\n str1 is Equal to str2");
}

return 0;
}

Output :-

10. Write a program to compare the two strings without using


strcmp() function.

= #include <stdio.h>
int main() {
char s1[100], s2[100], i;
printf("Enter string s1: ");
fgets(s1, sizeof(s1), stdin);

for (i = 0; s1[i] != '\0'; ++i) {


s2[i] = s1[i];
}

s2[i] = '\0';
printf("String s2: %s", s2);
return 0;
}

Output:-
11. Write a program to sort a set of string in ascending order
according to alphabet with the help of Bubble sort Technique.

= #include<stdio.h>
#include<string.h>
int main(){
int i,j,count;
char str[25][25],temp[25];
puts("How many strings you are going to enter: ");
scanf("%d",&count);

printf("Enter Strings %d string:\n",count);


for(i=0;i<=count;i++)
gets(str[i]);
for(i=0;i<=count;i++)
for(j=i+1;j<=count;j++){
if(strcmp(str[i],str[j])>0){
strcpy(temp,str[i]);
strcpy(str[i],str[j]);
strcpy(str[j],temp);
}
}
printf("Order of Sorted Strings:");
for(i=0;i<=count;i++)
puts(str[i]);

return 0;
}
Output:-

12. Write a program to convert any integer number in between 1 to


100 into its word expression.

= #include <stdio.h>
#include <stdlib.h>
#include <string.h>

void convert_to_words(char *num);

int main(){
char ch[100];
printf("Enter your number: ");
gets(ch);
convert_to_words(ch);
return 0;
}
void convert_to_words(char *num){
int len = strlen(num);

if (len == 0){
fprintf(stderr, "empty string\n");
return;
}
if (len > 4){
fprintf(stderr,
"Length more than 4 is not supported\n");
return;
}
char *single_digits[] = {"zero", "one", "two", "three", "four",
"five", "six", "seven", "eight", "nine"};

char *two_digits[] = {"", "ten", "eleven", "twelve",


"thirteen", "fourteen", "fifteen", "sixteen",
"seventeen", "eighteen", "nineteen"};

char *tens_multiple[] = {"", "", "twenty",


"thirty", "forty", "fifty",
"sixty", "seventy", "eighty",
"ninety"};

char *tens_power[] = {"hundred"};


printf("\n%s: ", num);
if(len == 1){
printf("%s\n", single_digits[*num - '0']);
return;
}
while (*num != '\0'){
if (len >= 3){
if (*num - '0' != 0){
printf("%s ", single_digits[*num - '0']);
printf("%s ",tens_power[len - 3]);
}
--len;
}

else{
if(*num == '1'){
int sum = *num - '0' + *(num + 1) - '0';
printf("%s\n", two_digits[sum]);
return;
}
else if (*num == '2' && *(num + 1) == '0'){
printf("twenty\n");
return;
}
else{
int i = *num - '0';
printf("%s ", i ? tens_multiple[i] : "");
++num;
if (*num != '0')
printf("%s ",single_digits[*num - '0']);
}
}
++num;
}
}

Output:-

You might also like