Find
the Most Least Repeated Character in the String
#include
#include
#include
#include
struct detail
{
char c;
int freq;
};
int main()
{
struct detail s[26];
char string[100], c;
int max[26] = {0}, min[26] = {0};
int i = 0, index, maxcount = 1, mincount = 1000, j;
clrscr();
for (i = 0; i < 26; i++)
{
s[i].c = i + 'a';
s[i].freq = 0;
}
printf("Enter string: ");
i = 0;
do
{
fflush(stdin);
c = getchar();
string[i++] = c;
if (c == '\n')
{
break;
}
else if (!isalpha(c))
{
continue;
}
c = tolower(c);
index = c - 'a';
s[index].freq++;
} while (1);
string[i - 1] = '\0';
printf("The string entered is: %s\n", string);
for (i = 0; i < 26; i++)
{
if (s[i].freq)
{
if (maxcount < s[i].freq)
{
for (j = 0; j < 26; j++)
{
max[j] = 0;
}
max[i] = 1;
maxcount = s[i].freq;
}
else if (maxcount == s[i].freq)
{
max[i] = 1;
}
if (mincount >= s[i].freq)
{
if (mincount == s[i].freq)
{
min[i] = 1;
}
else
{
for (j = 0; j < 26; j++)
{
min[j] = 0;
}
min[i] = 1;
mincount = s[i].freq;
}
}
}
}
printf("The most repeated characters are: ");
for (i = 0; i < 26; i++)
{
if (max[i])
{
printf("%c ", i + 'a');
}
}
printf("\nThe least repeated characters are: ");
for (i = 0; i < 26; i++)
{
if (min[i])
{
printf("%c ", i + 'a');
}
}
printf("\n");
return 0;
getch();
}
Output
Enter string: I love C programming
The string entered is: I love C programming
The most repeated characters are: g i m o r
The least repeated characters are: a c e l n p v
Implement the KMP Pattern Searching Algorithm
#include
#include
#include
#include
int main()
{
char string[100], matchcase[20], c;
int i = 0, j = 0, index;
clrscr();
printf("Enter string: ");
do
{
fflush(stdin);
c = getchar();
string[i++] = tolower(c);
} while (c != '\n');
string[i - 1] = '\0';
printf("Enter substring: ");
i = 0;
do
{
fflush(stdin);
c = getchar();
matchcase[i++] = tolower(c);
} while (c != '\n');
matchcase[i - 1] = '\0';
for (i = 0; i < strlen(string) - strlen(matchcase) + 1; i++)
{
index = i;
if (string[i] == matchcase[j])
{
do
{
i++;
j++;
} while(j != strlen(matchcase) && string[i] == matchcase[j]);
if (j == strlen(matchcase))
{
printf("Match found from position %d to %d.\n", index + 1, i);
return 0;
}
else
{
i = index + 1;
j = 0;
}
}
}
printf("No substring match found in the string.\n");
return 0;
getch();
}
Output
Enter string: programming
Enter substring: gram
Match found from position 4 to 7.
No comments:
Post a Comment