Some program for string in C

Q1) Find the Frequency of Characters

  1. #include <stdio.h>
  2. int main()
  3. {
  4. char str[1000], ch;
  5. int i, frequency = 0;
  6. printf("Enter a string: ");
  7. gets(str);
  8. printf("Enter a character to find the frequency: ");
  9. scanf("%c",&ch);
  10. for(i = 0; str[i] != '\0'; ++i)
  11. {
  12. if(ch == str[i])
  13. ++frequency;
  14. }
  15. printf("Frequency of %c = %d", ch, frequency);
  16. return 0;
  17. }
Output
Enter a string: This website is awesome.
Enter a character to find the frequency: e
Frequency of e = 4
In this program, the string entered by the user is stored in variable str.
Then, the user is asked to enter the character whose frequency is to be found. This is stored in variable ch.

Q2)Program to count vowels, consonants etc.

  1. #include <stdio.h>
  2. int main()
  3. {
  4. char line[150];
  5. int i, vowels, consonants, digits, spaces;
  6. vowels = consonants = digits = spaces = 0;
  7. printf("Enter a line of string: ");
  8. scanf("%[^\n]", line);
  9. for(i=0; line[i]!='\0'; ++i)
  10. {
  11. if(line[i]=='a' || line[i]=='e' || line[i]=='i' ||
  12. line[i]=='o' || line[i]=='u' || line[i]=='A' ||
  13. line[i]=='E' || line[i]=='I' || line[i]=='O' ||
  14. line[i]=='U')
  15. {
  16. ++vowels;
  17. }
  18. else if((line[i]>='a'&& line[i]<='z') || (line[i]>='A'&& line[i]<='Z'))
  19. {
  20. ++consonants;
  21. }
  22. else if(line[i]>='0' && line[i]<='9')
  23. {
  24. ++digits;
  25. }
  26. else if (line[i]==' ')
  27. {
  28. ++spaces;
  29. }
  30. }
  31. printf("Vowels: %d",vowels);
  32. printf("\nConsonants: %d",consonants);
  33. printf("\nDigits: %d",digits);
  34. printf("\nWhite spaces: %d", spaces);
  35. return 0;
  36. }
Output
Enter a line of string: adfslkj34 34lkj343 34lk
Vowels: 1
Consonants: 11
Digits: 9
White spaces: 2
This program takes string input from the user and stores in variable line.
Initially, the variables vowelsconsonantsdigits and spaces are initialized to 0.
When the vowel character is found, vowel variable is incremented by 1. Similarly, consonantsdigits and spaces are incremented when these characters are found.
Finally, the count is displayed on the screen.

Q3)Remove Characters in String Except Alphabets

  1. #include<stdio.h>
  2. int main()
  3. {
  4. char line[150];
  5. int i, j;
  6. printf("Enter a string: ");
  7. gets(line);
  8. for(i = 0; line[i] != '\0'; ++i)
  9. {
  10. while (!( (line[i] >= 'a' && line[i] <= 'z') || (line[i] >= 'A' && line[i] <= 'Z') || line[i] == '\0') )
  11. {
  12. for(j = i; line[j] != '\0'; ++j)
  13. {
  14. line[j] = line[j+1];
  15. }
  16. line[j] = '\0';
  17. }
  18. }
  19. printf("Output String: ");
  20. puts(line);
  21. return 0;
  22. }
Output
Enter a string: p2'r-o@gram84iz./
Output String: programiz
This program takes a string from the user and stored in the variable line.
The, within the for loop, each character in the string is checked if it's an alphabet or not.
If any character inside a string is not a alphabet, all characters after it including the null character is shifted by 1 position to the left.

No comments:

Post a Comment

Featured Post

Core and Advance Python

                      Language Fundamentals