Professional Documents
Culture Documents
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
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;
}
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;
i = 0;
words = 1;
i++;
}
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(){
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;
}
}
}
return 0;
}
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. }
#include <stdio.h>
#include <string.h>
int main()
{
char str[100], ch;
int i, len, j;
len = strlen(str);
return 0;
}
#include <stdio.h>
#include <string.h>
int main()
{
char str[100], result;
int i, len;
int max = -1;
len = strlen(str);
return 0;
}
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 main()
{
char a[100], b[100];
if (check_anagram(a, b) == 1)
printf(“The strings are anagrams\n”);
else
printf(“The strings are not anagrams\n”);
return 0;
}
c = 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;
}
}
}
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;
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);
}
#include <stdio.h>
int main() {
char str[1000], ch;
int count = 0;