Wednesday, 29 May 2019

Replace Mirror characters in a given string : C program

Given a string and a number N, we need to mirror the characters from N-th position
up to the length of the string in the alphabetical order.
In mirror operation,change ‘a’ to ‘z’, ‘b’ to ‘y’, and so on.

Using the fact that the distance from 'z' to that character and
distance from 'a' to mirror of the character are same.

Program:
#include <stdio.h>
main()
{
char s1[100];
scanf("%[^\n]",s1);
int i,n;
scanf("%d",&n);
for(i=n-1;s1[i]!='\0';i++)
s1[i]='z'-s1[i]+'a';
printf("%s",s1);
}

Output:
section
2
svxgrlm

WAP to Find Highest Frequency Character in a String

#include <stdio.h>
main()
{
char s1[100];
scanf("%[^\n]",s1);
int i,h[26] ={0};
for(i=0;s1[i] != '\0';i++)
h[s1[i] -97 ]++;
int max=0,index;
for(i=0;i<26;i++)
if(h[i]>max)
{
index=i;
max=h[i];
}
printf("%c occured %d times",index+97,max);
}

No comments:

Post a Comment