string programs

88
1. C Program to Check if a given String is Palindrome 2. C Program to read two Strings & Concatenate the Strings 3. C Program to Replace Lowercase Characters b !ppercase & "ice#"ersa $. C Program to Co%nt the %mber of "owels & Consonants in a Sentence '. C Program to Check if the S%bstring is present in the given String (. C Program to )ccepts two Strings & Compare them *. C Program to +ind the Length of a String witho%t %sing the ,%ilt#in -. C Program to Check if a String is a Palindrome witho%t %sing the ,%i +%nction . C Program to +ind the +re/%enc of the 0ord the in a given Sentenc 1 .C Program to Read a String and 4nd the S%m of all 5igits in the Str 11.C Program to Reverse the String %sing Rec%rsion 12.C Program to Cop 6ne String to )nother %sing Rec%rsion 13.C Program to 4nd the +irst Capital Letter in a String %sing Rec%rsio 1$.C Program to 4nd the +irst Capital Letter in a String witho%t %sing 1'.C Program to Check whether a given String is Palindrome or not %sing Rec%rsion 1(.C Program to Check whether two Strings are )nagrams 1*.C Program to Co%nt the 6cc%rence of a S%bstring in String 1-.C Program to Remove given 0ord from a String 1 .C Program to +ind 7ighest +re/%enc Character in a String 2 .C Program to 5ispla ever possible Combination of two 0ords or Str from the inp%t Strings witho%t Repeated Combinations 21.C Program to Reverse ever 0ord of given String 22.C Program to List )ll Lines containing a given String 23.C Program to Concatenate two Strings Le8icall 2$.C Program to Print the 0ords 9nding with Letter S 2'.C Program to +ind the S%m of )SC:: val%es of )ll Characters in a giv 2(.C Program to 4nd Longer Repeating Se/%ence 2*.C Program to Replace all the Characters b Lowercase 2-.C Program to Co%nt the ;otal %mber of 0ords in the Sentence %sing Command Line )rg%ment 2 .C Program to :nsert Character<0ord in an 5esired Location in a Stri 3 .C Program to +ind the +irst 6cc%rence of the an Character of Strin String1 31.C Program to :mplement Reg%lar 98pression =atching 32.C Program to :mplement strpbrk>? +%nction 33.C Program to Sort the String and Repeated Characters sho%ld be prese 6nce 3$.C Program to +ind the Length of the Longest Repeating Se/%ence in a 3'.C Program to Co%nt the %mber of !ni/%e 0ords 3(.C Program to 4nd the possible s%bsets of the String 3*.C Program to Search a 0ord & Replace it with the Speci4ed 0ord 3-.C Program to 4nd +irst and Last 6cc%rrence of given Character in a S 3 .C Program to 5ispla the Characters in Prime Position a given String $ .C Program to +ind )ll Possible S%bsets of given Length in String $1.C Program to 5elete )ll Repeated 0ords in String $2.C Program to Co%nt %mber of 0ords in a given ;e8t 6r Sentence

Upload: anupam-banerjee

Post on 04-Oct-2015

19 views

Category:

Documents


0 download

DESCRIPTION

C was originally developed by Dennis Ritchie between 1969 and 1973 at AT&T Bell Labs,[5] and used to (re-)implement the Unix operating system.[6] It has since become one of the most widely used programming languages of all time,[7][8] with C compilers from various vendors available for the majority of existing computer architectures and operating systems. C has been standardized by the American National Standards Institute (ANSI) since 1989 (see ANSI C) and subsequently by the International Organization for Standardization (ISO).

TRANSCRIPT

1. C Program to Check if a given String is Palindrome 2. C Program to read two Strings & Concatenate the Strings 3. C Program to Replace Lowercase Characters by Uppercase & Vice-Versa 4. C Program to Count the Number of Vowels & Consonants in a Sentence 5. C Program to Check if the Substring is present in the given String 6. C Program to Accepts two Strings & Compare them 7. C Program to Find the Length of a String without using the Built-in Function 8. C Program to Check if a String is a Palindrome without using the Built-in Function 9. C Program to Find the Frequency of the Word the in a given Sentence 10. C Program to Read a String and find the Sum of all Digits in the String 11. C Program to Reverse the String using Recursion12. C Program to Copy One String to Another using Recursion13. C Program to find the First Capital Letter in a String using Recursion14. C Program to find the First Capital Letter in a String without using Recursion15. C Program to Check whether a given String is Palindrome or not using Recursion16. C Program to Check whether two Strings are Anagrams17. C Program to Count the Occurence of a Substring in String 18. C Program to Remove given Word from a String 19. C Program to Find Highest Frequency Character in a String 20. C Program to Display every possible Combination of two Words or Strings from the input Strings without Repeated Combinations 21. C Program to Reverse every Word of given String 22. C Program to List All Lines containing a given String 23. C Program to Concatenate two Strings Lexically 24. C Program to Print the Words Ending with Letter S 25. C Program to Find the Sum of ASCII values of All Characters in a given String 26. C Program to find Longer Repeating Sequence 27. C Program to Replace all the Characters by Lowercase 28. C Program to Count the Total Number of Words in the Sentence using Command Line Argument 29. C Program to Insert Character/Word in any Desired Location in a String 30. C Program to Find the First Occurence of the any Character of String2 in String1 31. C Program to Implement Regular Expression Matching 32. C Program to Implement strpbrk() Function 33. C Program to Sort the String and Repeated Characters should be present only Once 34. C Program to Find the Length of the Longest Repeating Sequence in a String 35. C Program to Count the Number of Unique Words 36. C Program to find the possible subsets of the String 37. C Program to Search a Word & Replace it with the Specified Word 38. C Program to find First and Last Occurrence of given Character in a String 39. C Program to Display the Characters in Prime Position a given String 40. C Program to Find All Possible Subsets of given Length in String 41. C Program to Delete All Repeated Words in String 42. C Program to Count Number of Words in a given Text Or Sentence 43. C Program to Find the Frequency of Substring in the given String 44. C Program to Find the Frequency of Every Word in a given String 45. C Program to Sort Word in String 46. C Program To Print Smallest and Biggest possible Word which is Palindrome in a given String 47. C Program to Print Combination of two Words of two given Strings without any Repetition 48. C Program to Sort String Ignoring Whitespaces and Repeating Characters Only Once 49. C Program to Reverse the String using Both Recursion and Iteration 50. C Program to Display Every Possible Combination of Two Words from the given 2 String without displaying Repeated Combinations 51. C Program to Accept 2 String & check whether all Characters in first String is Present in second String & Print 52. C Program to Check whether a given Character is present in a String, Find Frequency & Position of Occurrence 53. C Program to Count the Number of Occurrence of each Character Ignoring the Case of Alphabets Display them 54. C Program to Count the Occurrences of each C Keyword using Array Structure 55. C Program to Determine if One String is a Circular Permutation of Another String 56. C Program to Find the Consecutive Occurrence of any Vowel in a String 57. C Program to Find the Largest & Smallest Word in a String 58. C Program to Find the Most/Least Repeated Character in the String 59. C Program to Implement the KMP Pattern Searching Algorithm 60. C Program to Input 2 Binary Strings and Print their Binary Sum 61. C Program to Input a String with at least one Number, Print the Square of all the Numbers in a string 62. C Program to Remove all Characters in Second String which are present in First String 63. C Program which Converts an Integer to String & vice-versa 64. C program that takes input as 2323 and gives output as 2332. ie. the new number should be greater than the previous number but should have the same digits

C Program to Check if a given String is PalindromeThis C Program checks if a given string is palindrome. This program first performs reverse of a string. Then it checks whether the given string is equivalent to the reversed string. If they are equal then the string is called as palindrome.Here is source code of the C program to check a given string is palindrome. The C program is successfully compiled and run on a Linux system. The program output is also shown below. 1. /*2. * C program to read a string and check if it's a palindrome, without3. * using library functions. Display the result.4. */5. #include 6. #include 7. 8. void main()9. {10. char string[25], reverse_string[25] = {'\0'};11. int i, length = 0, flag = 0;12. 13. fflush(stdin);14. printf("Enter a string \n");15. gets(string);16. /* keep going through each character of the string till its end */17. for (i = 0; string[i] != '\0'; i++)18. {19. length++;20. }21. for (i = length - 1; i >= 0; i--)22. {23. reverse_string[length - i - 1] = string[i];24. }25. /*26. * Compare the input string and its reverse. If both are equal27. * then the input string is palindrome.28. */29. for (i = 0; i < length; i++)30. {31. if (reverse_string[i] == string[i])32. flag = 1;33. else34. flag = 0;35. }36. if (flag == 1)37. printf("%s is a palindrome \n", string);38. else39. printf("%s is not a palindrome \n", string);40. }

C Program to read two Strings & Concatenate the StringsThis C Program reads the two strings & concatenate the strings without using string library functions. This program first reads the 2 strings using scanf(), then joins the one string with another. Later it reads and prints it as a single string.Here is source code of the C program to read two strings & concatenate the strings. The C program is successfully compiled and run on a Linux system. The program output is also shown below. 1. /*2. * C program to read two strings and concatenate them, without using3. * library functions. Display the concatenated string.4. */5. #include 6. #include 7. 8. void main()9. {10. char string1[20], string2[20];11. int i, j, pos;12. 13. /* Initialize the string to NULL values */14. memset(string1, 0, 20);15. memset(string2, 0, 20);16. 17. printf("Enter the first string : ");18. scanf("%s", string1);19. printf("Enter the second string: ");20. scanf("%s", string2);21. printf("First string = %s\n", string1);22. printf("Second string = %s\n", string2);23. 24. /* Concate the second string to the end of the first string */25. for (i = 0; string1[i] != '\0'; i++)26. {27. /* null statement: simply traversing the string1 */28. ;29. }30. pos = i;31. for (j = 0; string2[j] != '\0'; i++)32. {33. string1[i] = string2[j++];34. }35. /* set the last character of string1 to NULL */36. string1[i] = '\0';37. printf("Concatenated string = %s\n", string1);38. }

C Program to Replace Lowercase Characters by Uppercase & Vice-VersaThis C Program replaces lowercase characters by uppercase & vice-versa. This program checks the each character of a string one by one. If the character is in lowercase then converts it into uppercase & if it is in uppercase then converts into lowercase.Here is source code of the C program to replace lowercase characters by uppercase & vice-versa. The C program is successfully compiled and run on a Linux system. The program output is also shown below. 1. /*2. * C program to read an English sentence and replace3. * lowercase characters by uppercase and vice-versa.4. * Output the given sentence as well as the converted5. * sentence on two different lines.6. */7. #include 8. #include 9. 10. void main()11. {12. char sentence[100];13. int count, ch, i;14. 15. printf("Enter a sentence \n");16. for (i = 0; (sentence[i] = getchar()) != '\n'; i++)17. {18. ;19. }20. sentence[i] = '\0';21. /* shows the number of chars accepted in a sentence */22. count = i;23. printf("The given sentence is : %s", sentence);24. printf("\n Case changed sentence is: ");25. for (i = 0; i < count; i++)26. {27. ch = islower(sentence[i])? toupper(sentence[i]) :28. tolower(sentence[i]);29. putchar(ch);30. }31. }

C Program to Count the Number of Vowels & Consonants in a SentenceThis C Program counts the number of vowels & consonants in a sentence. This program is used to find the number of vowels and consonants present in the given sentence. The vowels are a, e , i, o, u, The remaining letters are consonants.Here is source code of the C program to count the number of vowels & consonants in a sentence. The C program is successfully compiled and run on a Linux system. The program output is also shown below. 1. /*2. * C program to read a sentence and count the total number of vowels3. * and consonants in the sentence.4. */5. #include 6. 7. void main()8. {9. char sentence[80];10. int i, vowels = 0, consonants = 0, special = 0;11. 12. printf("Enter a sentence \n");13. gets(sentence);14. for (i = 0; sentence[i] != '\0'; i++)15. {16. if ((sentence[i] == 'a' || sentence[i] == 'e' || sentence[i] ==17. 'i' || sentence[i] == 'o' || sentence[i] == 'u') ||18. (sentence[i] == 'A' || sentence[i] == 'E' || sentence[i] ==19. 'I' || sentence[i] == 'O' || sentence[i] == 'U'))20. {21. vowels = vowels + 1;22. }23. else24. {25. consonants = consonants + 1;26. }27. if (sentence[i] =='t' ||sentence[i] =='\0' || sentence[i] ==' ')28. {29. special = special + 1;30. }31. }32. consonants = consonants - special;33. printf("No. of vowels in %s = %d\n", sentence, vowels);34. printf("No. of consonants in %s = %d\n", sentence, consonants);35. }

C Program to Check if the Substring is present in the given StringThis C Program checks the substring is present in the given string. The program accepts both string and substring. Then checks whether the substring is present in the given string.Here is source code of the C program to check the substring is present in the given string. The C program is successfully compiled and run on a Linux system. The program output is also shown below. 1. /*2. * C program to accept a string and a substring and3. * check if the substring is present in the given string4. */5. #include6. 7. void main()8. {9. char str[80], search[10];10. int count1 = 0, count2 = 0, i, j, flag;11. 12. printf("Enter a string:");13. gets(str);14. printf("Enter search substring:");15. gets(search);16. while (str[count1] != '')17. count1++;18. while (search[count2] != '')19. count2++;20. for (i = 0; i string2 then display a appropriate message and so on.Here is source code of the C program to accepts two strings & compare them. The C program is successfully compiled and run on a Linux system. The program output is also shown below. 1. /*2. * C Program to accepts two strings and compare them. Display 3. * the result whether both are equal, or first string is greater 4. * than the second or the first string is less than the second string5. */6. #include 7. 8. void main()9. {10. int count1 = 0, count2 = 0, flag = 0, i;11. char string1[10], string2[10];12. 13. printf("Enter a string:");14. gets(string1);15. printf("Enter another string:");16. gets(string2);17. /* Count the number of characters in string1 */18. while (string1[count1] != '\0')19. count1++;20. /* Count the number of characters in string2 */21. while (string2[count2] != '\0')22. count2++;23. i = 0;24. 25. while ((i < count1) && (i < count2))26. {27. if (string1[i] == string2[i])28. {29. i++;30. continue;31. }32. if (string1[i] < string2[i])33. {34. flag = -1;35. break;36. }37. if (string1[i] > string2[i])38. {39. flag = 1;40. break;41. }42. }43. if (flag == 0)44. printf("Both strings are equal \n");45. if (flag == 1)46. printf("String1 is greater than string2 \n", string1, string2);47. if (flag == -1)48. printf("String1 is less than string2 \n", string1, string2);49. }

C Program to Find the Length of a String without using the Built-in FunctionThis C Program finds the length of a string without using the built-in function. This program is used to find the length of the string means total number of characters present in a given string. This program need to be done without using the built-in-function. The built in functions are those which have built- in already which can be used directly.Here is source code of the C program to finds the length of a string without using the built-in function..The C program is successfully compiled and run on a Linux system. The program output is also shown below. 1. /*2. * C program to find the length of a string without using the3. * built-in function4. */5. #include 6. 7. void main()8. {9. char string[50];10. int i, length = 0;11. 12. printf("Enter a string \n");13. gets(string);14. /* keep going through each character of the string till its end */15. for (i = 0; string[i] != '\0'; i++)16. {17. length++;18. }19. printf("The length of a string is the number of characters in it \n");20. printf("So, the length of %s = %d\n", string, length); }21.

C Program to Check if a String is a Palindrome without using the Built-in FunctionThis C Program checks a given string is palindrome without using the Built-in Function. This program first performs reverse of a string. Then it checks whether the given string is equivalent to the reversed string. If they are equal then the string is called as palindrome. The program doesnt use any built-in function.Here is source code of the C program to check a given string is palindrome without using the Built-in Function .The C program is successfully compiled and run on a Linux system. The program output is also shown below. 1. /*2. * C program to find the length of a string without using the3. * built-in function also check whether it is a palindrome4. */5. #include 6. #include 7. 8. void main()9. {10. char string[25], reverse_string[25] = {'\0'};11. int i, length = 0, flag = 0;12. 13. printf("Enter a string \n");14. gets(string);15. /* keep going through each character of the string till its end */16. for (i = 0; string[i] != '\0'; i++)17. {18. length++;19. }20. printf("The length of the string '%s' = %d\n", string, length);21. for (i = length - 1; i >= 0 ; i--)22. {23. reverse_string[length - i - 1] = string[i];24. }25. /* Check if the string is a Palindrome */26. 27. for (flag = 1, i = 0; i < length ; i++)28. {29. if (reverse_string[i] != string[i])30. flag = 0;31. }32. if (flag == 1)33. printf ("%s is a palindrome \n", string);34. else35. printf("%s is not a palindrome \n", string);36. }C Program to Find the Frequency of the Word the in a given SentenceThis C Program finds the frequency of the word the in a given sentence. This program is used find the occurance of the word the present in a given sentence. And displays number of times the word has occured.Here is source code of the C program to finds the frequency of the word the in a given sentence. The C program is successfully compiled and run on a Linux system. The program output is also shown below. 1. /*2. * C program to accept a string and find the number of times the word3. * 'the' appears in that string4. */5. #include 6. 7. void main()8. {9. int count = 0, i, times = 0, t, h, e, space;10. char string[100];11. 12. puts("Enter a string:");13. gets(string);14. /* Traverse the string to count the number of characters */15. while (string[count] != '\0')16. {17. count++;18. }19. /* Finding the frequency of the word 'the' */20. for (i = 0; i = '0') && (string[count] %d times\n", ptr1[i], c);64. c = 0;65. }66. }

C Program to Sort Word in StringThis C Program Sort Word in String.Here is source code of the C Program to Sort Word in String. The C program is successfully compiled and run on a Linux system. The program output is also shown below. 1. /*2. * C Program to Sort Word in String3. */4. #include 5. #include 6. 7. void main()8. {9. int count = 0, c = 0, i, j = 0, k, l, space = 0;10. char str[100], p[50][100], str1[20], ptr1[50][100], cmp[50];11. 12. printf("Enter the string\n");13. scanf(" %[^\n]s", str);14. for (i = 0;i < strlen(str);i++)15. {16. if ((str[i] == ' ')||(str[i] == ', ')||(str[i] == '.'))17. {18. space++;19. }20. }21. for (i = 0, j = 0, k = 0;j < strlen(str);j++)22. {23. if ((str[j] == ' ')||(str[j] == 44)||(str[j] == 46)) 24. { 25. p[i][k] = '\0';26. i++;27. k = 0;28. } 29. else30. p[i][k++] = str[j];31. }32. for (i = 0;i < space;i++) //loop for sorting33. {34. for (j = i + 1;j 0))37. {38. strcpy(cmp, p[i]);39. strcpy(p[i], p[j]);40. strcpy(p[j], cmp);41. }42. }43. }44. printf("After sorting string is \n");45. for (i = 0;i = max)64. strcpy(maxP, d);65. else if (len string[j])33. {34. temp = string[i];35. string[i] = string[j];36. string[j] = temp;37. }38. }39. }40. string[i] = '\0';41. len = strlen(string);42. 43. /* Code to remove redundant characters */44. for (i = 0;string[i] != '\0';i++)45. {46. if (string[i] == string[i + 1] && string[i + 1] != '\0')47. {48. k++;49. continue;50. }51. string2[a++] = string[i];52. string[a] = '\0';53. }54. len1 = len - k;55. printf("The sorted string is:");56. for (temp = 0;temp < len1;temp++)57. {58. printf("%c", string2[temp]);59. }60. }

C Program to Reverse the String using Both Recursion and IterationThis C Program reverse the string using both recursion and iteration.Here is source code of the C Program to reverse the string using both recursion and iteration. The C program is successfully compiled and run on a Linux system. The program output is also shown below. 1. /*2. * C Program to Reverse the String using Both Recursion and Iteration3. */4. #include 5. #include 6. 7. /* Function Prototype */8. void disp_str1_rec(char *);9. 10. void main()11. {12. char str1[100], *ptr;13. int len1 = 0, i;14. char ch;15. printf("Enter the string:\n");16. scanf("%[^\n]s", str1);17. ptr = str1;18. len1 = strlen(str1);19. printf("Using iteration:\n");20. for (i = len1 - 1; i >= 0;i--) /* Iterative loop */21. {22. 23. ch = str1[i];24. printf("%c", ch);25. }26. printf("Using recurssion:\n");27. disp_str1_rec(ptr);28. }29. 30. /* Code to reverse the string using Recursion */31. void disp_str1_rec(char *stng)32. {33. char ch;34. if (*stng != '\0')35. {36. ch = *stng;37. stng++;38. disp_str1_rec(stng);39. printf("%c", ch);40. }41. else42. return;43. }

C Program to Display Every Possible Combination of Two Words from the given 2 String without Displaying Repeated CombinationsThis C Program Displays Every Possible Combination of Two Words from the given 2 String without Displaying Repeated Combinations.Here is source code of the C Program to Display Every Possible Combination of Two Words from the given 2 String without Displaying Repeated Combinations. The C program is successfully compiled and run on a Linux system. The program output is also shown below. 1. /*2. * C Program to Display Every Possible Combination of Two Words 3. * from the given 2 String without Displaying Repeated Combinations4. */5. #include 6. #include 7. 8. void main()9. {10. char str1[50], str2[50], str3[100][100], str4[100][100];11. char str5[200][200], temp[200], str[200][200];12. int i, j = 0, k = 0, l = 0, m = 0, index = 0, n = 0;13. printf("Enter first string\n");14. scanf("%[^\n]s", str1);15. printf("Enter second string\n");16. scanf(" %[^\n]s", str2); 17. 18. /* code to convert string in 2-D array */19. for (i = 0;str1[i] != '\0';i++)20. { 21. if ((str1[i] = = ' ')22. {23. str3[j][k] = '\0';24. j++;25. k = 0;26. }27. else28. {29. str3[j][k] = str1[i];30. k++;31. }32. str3[j][k] = '\0'; 33. }34. k = 0;35. 36. for (i = 0;str2[i] != '\0';i++)37. {38. if ((str2[i] == ' ')39. {40. str4[l][k] = '\0';41. l++;42. k = 0;43. }44. else45. {46. str4[l][k] = str2[i];47. k++;48. }49. str4[l][k] = '\0';50. }51. /* Code to make the first string words combination with second */52. for (i = 0;i digit[i + 1])47. {48. temp = digit[i];49. digit[i] = digit[i + 1];50. digit[i + 1] = temp;51. 52. return (evaluate(digit, len));53. }54. }55. 56. return 0;57. }58. 59. int evaluate(int digit[], int len)60. {61. int i, num = 0;62. 63. for (i = 0; i < len; i++)64. {65. num += digit[i] * pow(10, i);66. }67. 68. return num;69. }

$ gcc greaternum.c -lm$ ./a.outEnter a number: 56732 The number greater than 56732 and made of same digits is 57632.