• codes for entertainment purpose


    1.calculates and prints the number of and sum of the integers between 1 and 100 (inclusive) that are not divisible by 3 or 5

    codes:

    1. #include<stdio.h>
    2. int main(){
    3. int i;
    4. int sum=0;
    5. int count=0;
    6. for(i=1;i<=100;i++){
    7. if(i%3!=0 && i%5!=0){
    8. sum=sum+i;
    9. count=count+1;
    10. }
    11. }
    12. printf("sum=%d number=%d\n",sum,count);
    13. return 0;
    14. }

    2.The program prompts for a string (max 80 characters) and converts uppercase characters to lowercase

    codes:

    1. #include <stdio.h>
    2. #include <string.h>
    3. int main(void) {
    4. char str[81];
    5. int i, len;
    6. printf("Enter string:\n");
    7. fgets(str, 81, stdin);
    8. len = strlen(str);
    9. if(str[len-1] == '\n') {
    10. str[len-1] = 0;
    11. }
    12. for(i=0; str[i]; i++) {
    13. if(str[i] >= 'A' && str[i] <= 'Z') {
    14. str[i] += 32;
    15. }
    16. }
    17. printf("%s\n", str);
    18. return 0;
    19. }

    3.Convert lowercase characters to uppercase instead.

    codes:

    1. #include <stdio.h>
    2. #include <string.h>
    3. int main(void) {
    4. char str[81];
    5. int i, len;
    6. printf("Enter string:\n");
    7. fgets(str, 81, stdin);
    8. len = strlen(str);
    9. if(str[len-1] == '\n') {
    10. str[len-1] = 0;
    11. }
    12. for(i=0; str[i]; i++) {
    13. if(str[i] >= 'a' && str[i] <= 'z') {
    14. str[i] -= 32;
    15. }
    16. }
    17. printf("%s\n", str);
    18. return 0;
    19. }

    4.Count the number of each type of letter (A to Z) and print a table showing these counts. Ignore non-alphabetic characters.

    codes:

    1. #include <stdio.h>
    2. #include <string.h>
    3. int main(void) {
    4. char str[81];
    5. int lettercount[26];
    6. int i, len;
    7. for(i=0; i<26; i++) {
    8. lettercount[i] = 0;
    9. }
    10. printf("Enter string:\n");
    11. fgets(str, 81, stdin);
    12. len = strlen(str);
    13. if(str[len-1] == '\n') {
    14. str[len-1] = 0;
    15. }
    16. for(i=0; str[i]; i++) {
    17. if(str[i] >= 'A' && str[i] <= 'Z') {
    18. lettercount[str[i]-'A']++;
    19. } else if(str[i] >= 'a' && str[i] <= 'z') {
    20. lettercount[str[i]-'a']++;
    21. }
    22. }
    23. for(i=0; i<26; i++) {
    24. printf("%c: %d\n", 'A'+i, lettercount[i]);
    25. }
    26. return 0;
    27. }

    5.Accept 20 lines of text (each up to 80 characters) instead of 1 line, then print a count of each letter seen (the total over all lines).

    codes:

    1. #include <stdio.h>
    2. #include <string.h>
    3. int main(void) {
    4. char str[81];
    5. int lettercount[26];
    6. int i, len;
    7. int linecount;
    8. for(i=0; i<26; i++) {
    9. lettercount[i] = 0;
    10. }
    11. for(linecount=0; linecount<20; linecount++) {
    12. printf("Enter string:\n");
    13. fgets(str, 81, stdin);
    14. len = strlen(str);
    15. if(str[len-1] == '\n') {
    16. str[len-1] = 0; /* Null character - we could also have said '\0' */
    17. }
    18. for(i=0; str[i]; i++) {
    19. if(str[i] >= 'A' && str[i] <= 'Z') {
    20. lettercount[str[i]-'A']++;
    21. } else if(str[i] >= 'a' && str[i] <= 'z') {
    22. lettercount[str[i]-'a']++;
    23. }
    24. }
    25. }
    26. for(i=0; i<26; i++) {
    27. printf("%c: %d\n", 'A'+i, lettercount[i]);
    28. }
    29. return 0;
    30. }

    6.As 5, but the program accepts any number of lines of text, up to when it sees a line containing only the text “END” (without the quotes)

    codes:

    1. #include <stdio.h>
    2. #include <string.h>
    3. int main(void) {
    4. char str[81];
    5. int lettercount[26];
    6. int i, len;
    7. int finished=0;
    8. for(i=0; i<26; i++) {
    9. lettercount[i] = 0;
    10. }
    11. while(!finished) {
    12. printf("Enter string:\n");
    13. fgets(str, 81, stdin);
    14. len = strlen(str);
    15. if(str[len-1] == '\n') {
    16. str[len-1] = 0;
    17. }
    18. if(strcmp(str, "END") == 0) {
    19. break;
    20. }
    21. for(i=0; str[i]; i++) {
    22. if(str[i] >= 'A' && str[i] <= 'Z') {
    23. lettercount[str[i]-'A']++;
    24. } else if(str[i] >= 'a' && str[i] <= 'z') {
    25. lettercount[str[i]-'a']++;
    26. }
    27. }
    28. }
    29. for(i=0; i<26; i++) {
    30. printf("%c: %d\n", 'A'+i, lettercount[i]);
    31. }
    32. return 0;
    33. }

  • 相关阅读:
    Java:SpringBoot统一异常处理和404异常
    系统篇: ubuntu 18.04 ROS1 和 ROS2 环境搭建
    Head First设计模式(阅读笔记)-06.命令模式
    专利申请怎样做快速预审?
    初创公司用“豆包”大模型,日均tokens两个月内增长357倍
    21天学习挑战赛--第一天打卡(屏幕密度)
    运行.sln 32/64位程序,启动不了,无法显示界面
    SpringBoot-黑马程序员-学习笔记(四)
    Unity SKFramework框架(二十)、VFX Lab 特效库
    java 部署docker pom.xml文件docker.host配置
  • 原文地址:https://blog.csdn.net/weixin_61023150/article/details/133432418