You are on page 1of 14

List of Programs :

1. Write a program to find the largest palindrome substring.

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

static inline int min(int x, int y)


{
return x < y ? x : y;
}
static int find_longest_substring(char *s, char output[])
{
int i, j;
char s2[3000] = { '\0' };

s2[0] = '$';
for (i = 0; s[i] != '\0'; i++) {
s2[(i<<1)+1] = '#';
s2[(i<<1)+2] = s[i];
}
s2[(i<<1)+1] = '#';
int len = (i<<1)+2;
s2[len] = '\0';

int p[3000] = { 0 };
int id = 0;
int limit = 0;
int max_len = 0;
int max_id = 0;
for (i = 1; i < len; i++) {
if (i < limit) {
p[i] = min(p[2*id-i], limit-i);
} else {
p[i] = 1;
}

while (s2[i+p[i]] == s2[i-p[i]]) {


p[i]++;
}

if (i+p[i] > limit) {


limit = i+p[i];
id = i;
}
if (max_len < p[i]-1) {
max_len = p[i]-1;
max_id = i;
}
}

for (j = 0, i = max_id - max_len; i <= max_id+max_len; i++) {


if (s2[i] != '#') {
output[j++] = s2[i];
}
}
return max_len;
}

static char *longest_Palindrom(char *s)


{
int i;
if (s == NULL) {
return NULL;
}

int len = strlen(s);


if (len <= 1) {
return s;
}

char *palindrome_str = malloc(2000);


memset(palindrome_str, 0, sizeof(palindrome_str));

int size = find_longest_substring(s, palindrome_str);


palindrome_str[size] = '\0';
return palindrome_str;
}

int main(void)
{
char *str1 = "yxypxst";
printf("\nOriginal String: %s", str1);
printf("\nLongest palindromic substring of the said string: %s\
n", longest_Palindrom(str1));
return 0;
}
2. Write a C program to count the total number of words in a string.

#include <stdio.h>
#define MAX_SIZE 100 // Maximum string size

int main()
{
char str[MAX_SIZE];
int i, words;

/* Input string from user */


printf("Enter any string: ");
gets(str);

i = 0;
words = 1;

/* Runs a loop till end of string */


while(str[i] != '\0')
{
/* If the current character(str[i]) is white space */
if(str[i]==' ' || str[i]=='\n' || str[i]=='\t')
{
words++;
}

i++;
}

printf("Total number of words = %d", words);

return 0;
}

3. Write a program in C to read a string through the keyboard and sort it using
bubble sort.

#include<stdio.h>

int main(){

int count, temp, i, j, number[30];

printf("How many numbers are u going to enter?: ");


scanf("%d",&count);

printf("Enter %d numbers: ",count);

for(i=0;i<count;i++)
scanf("%d",&number[i]);
/* This is the main logic of bubble sort algorithm
*/
for(i=count-2;i>=0;i--){
for(j=0;j<=i;j++){
if(number[j]>number[j+1]){
temp=number[j];
number[j]=number[j+1];
number[j+1]=temp;
}
}
}

printf("Sorted elements: ");


for(i=0;i<count;i++)
printf(" %d",number[i]);

return 0;
}

4. Program to reverse the order of words in a given string.

1. #include <stdio.h>
2. #include <string.h>
3.
4. void main()
5. {
6. int i, j = 0, k = 0, x, len;
7. char str[100], str1[10][20], temp;
8.
9. printf("enter the string :");
10. scanf("%[^\n]s", str);
11.
12. /* reads into 2d character array */
13. for (i = 0;str[i] != '\0'; i++)
14. {
15. if (str[i] == ' ')
16. {
17. str1[k][j]='\0';
18. k++;
19. j=0;
20. }
21. else
22. {
23. str1[k][j]=str[i];
24. j++;
25. }
26. }
27. str1[k][j] = '\0';
28.
29. /* reverses each word of a given string */
30. for (i = 0;i <= k;i++)
31. {
32. len = strlen(str1[i]);
33. for (j = 0, x = len - 1;j < x;j++,x--)
34. {
35. temp = str1[i][j];
36. str1[i][j] = str1[i][x];
37. str1[i][x] = temp;
38. }
39. }
40. for (i = 0;i <= k;i++)
41. {
42. printf("%s ", str1[i]);
43. }
44. }

5. C program to remove all occurrences of a character from the given string.

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

int main()
{
char str[100], ch;
int i, len, j;

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


gets(str);

printf("\n Please Enter the Character that you want to


Remove : ");
scanf("%c", &ch);

len = strlen(str);

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


{
if(str[i] == ch)
{
for(j = i; j < len; j++)
{
str[j] = str[j + 1];
}
len--;
i--;
}
}
printf("\n The Final String after Removing All Occurrences of
'%c' = %s ", ch, str);

return 0;
}

6. C program to find maximum occurring character in a string

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

int main()
{
char str[100], result;
int i, len;
int max = -1;

int freq[256] = {0};

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


gets(str);

len = strlen(str);

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


{
freq[str[i]]++;
}

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


{
if(max < freq[str[i]])
{
max = freq[str[i]];
result = str[i];
}
}
printf("\n The Maximum Occurring Character in a Given String = %c
", result);

return 0;
}

7. C program to remove all repeated characters from a given string.

1. #include <stdio.h>
2. #include <string.h>
3.
4. int main()
5. {
6. char string[]="haiiiiiii hello";
7. char rem[20];
8. int i,j,lastpos=-1;
9.
10. for(i=0;i<strlen(string);++i)
11. {
12. for(j=0;j<=lastpos;++j)
13. if(string[i]==rem[j])
14. break;
15. if(j>lastpos)
16. {
17. lastpos++;
18. rem[lastpos]=string[i];
19. }
20.
21. }
22. ++lastpos;
23. rem[lastpos]='\0';
24. printf(" String: %s \n duplicate removed string:
%s",string,rem);
25. return 0;
26. }
8. To check whether two Strings are equal or not.

4 #include <stdio.h>
5 #include <string.h>
6 int main()
7 {
8 char s1[1000],s2[1000];
9 int i,c=0;
10 printf("Enter string1: ");
11 gets(s1);
12 printf("Enter string2: ");
13 gets(s2);
14 if(strlen(s1)==strlen(s2))
15 {
16 for(i=0;s2[i]!='\0';i++)
17 {
18 if(s1[i]==s2[i])
19 c++;
20 }
21 if(c==i)
22 printf("strings are equal");
23 else
24 printf("strings are not equal");
25 }
26 else
27 printf("strings are not equal");
28
29
30
31
32 return 0;
}

9. Take two strings and check if they have the same letters with the same
frequency.
#include <stdio.h>

int check_anagram(char [], char []);

int main()
{
char a[100], b[100];

printf(“Enter two strings : \n”);


gets(a);
gets(b);

if (check_anagram(a, b) == 1)
printf(“The strings are anagrams\n”);
else
printf(“The strings are not anagrams\n”);

return 0;
}

int check_anagram(char a[], char b[])


{
int first[26] = {0}, second[26] = {0}, c=0;

// Calculating frequency of characters of first string

while (a[c] != ‘\0’)


{
first[a[c]-‘a’]++;
c++;
}

c = 0;

while (b[c] != ‘\0’)


{
second[b[c]-‘a’]++;
c++;
}

// Comparing frequency of characters


for (c = 0; c < 26; c++)
{
if (first[c] != second[c])
return 0;
}

return 1;
}

10. Take a string input, and print the letters in it in the alphabetical
order.

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

int main ()
{
char string[100];
printf("\n\t Enter the string : ");
scanf(“%s”,string);
char temp;
int i, j;
int n = strlen(string);
for (i = 0; i < n-1; i++) {
for (j = i+1; j < n; j++) {
if (string[i] > string[j]) {
temp = string[i];
string[i] = string[j];
string[j] = temp;
}
}
}

printf(“The sorted string is : %s”, string);


return 0;
}
11. Rearrange all the letters of a word in ascending order while removing the
duplicate letters.

import java.util.*;
import java.lang.*;
public class Solution {
public static void main(String[] args) {
String str = "zxywooxz";
System.out.print("Original string: " + str);
System.out.print("\nAfter removing duplicate characters: " +
remove_duplicate_letters(str));
}
public static String remove_duplicate_letters(String str1) {
int[] ctr = new int[26];
boolean[] in_stack = new boolean[26];
char[] st_char = new char[str1.length()];
int len = 0;

for (char c: str1.toCharArray()) {


ctr[c - 'a']++;
}

for (char c: str1.toCharArray()) {


ctr[c - 'a']--;

if (!in_stack[c - 'a']) {
while (len > 0 && c < st_char[len - 1] && ctr[st_char[len - 1]
- 'a'] > 0) {
in_stack[st_char[--len] - 'a'] = false;
}
st_char[len++] = c;
in_stack[c - 'a'] = true;
}
}
return new String(st_char, 0, len);
}
}
12. Write a program to reverse all words in a sentence.

1. #include <stdio.h>
2. #include <string.h>
3.
4. void main()
5. {
6. int i, j = 0, k = 0, x, len;
7. char str[100], str1[10][20], temp;
8.
9. printf("enter the string :");
10. scanf("%[^\n]s", str);
11.
12. /* reads into 2d character array */
13. for (i = 0;str[i] != '\0'; i++)
14. {
15. if (str[i] == ' ')
16. {
17. str1[k][j]='\0';
18. k++;
19. j=0;
20. }
21. else
22. {
23. str1[k][j]=str[i];
24. j++;
25. }
26. }
27. str1[k][j] = '\0';
28. /* reverses each word of a given string */
29. for (i = 0;i <= k;i++)
30. {
31. len = strlen(str1[i]);
32. for (j = 0, x = len - 1;j < x;j++,x--)
33. {
34. temp = str1[i][j];
35. str1[i][j] = str1[i][x];
36. str1[i][x] = temp;
37. }
38. }
39. for (i = 0;i <= k;i++)
40. {
41. printf("%s ", str1[i]);
42. }
43. }
13. Program to convert all vowels into capital in a sentence.
#include <stdio.h>
int main()
{
char str[255];
int i;
printf("enter a string: ");
gets(str);
i=0;
while(str[i]!='\0')
{
if(str[i]=='a' ||str[i]=='e' ||
str[i]=='i' ||str[i]=='o' ||str[i]=='u')
str[i]=str[i]-32;
i++;
}
printf("converted string is:%s",str);
}

14. C program to find the occurrences of each character in a


pattern.

#include <stdio.h>
int main() {
char str[1000], ch;
int count = 0;

printf("Enter a string: ");


fgets(str, sizeof(str), stdin);

printf("Enter a character to find its frequency: ");


scanf("%c", &ch);

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


if (ch == str[i])
++count;
}

printf("Frequency of %c = %d", ch, count);


return 0;
}

You might also like