2024年2月8日

1.现有文件test.c\test1.c\main.c,请编写Makefile


2.C编程实现:输入字符串,请计算单词的个数

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

int count_words(char *str) {
    int count = 0;
    int in_word = 0;
    while (*str) {
        if (*str == ' ' || *str == '\n' || *str == '\t') {
            in_word = 0;
        } else if (!in_word) {
            in_word = 1;
            ++count;
        }
        ++str;
    }
    return count;
}

int main() {
    char str[] = "this is a boy";
    printf("输入:%s,输出单词个数:%d个\n", str, count_words(str));
    return 0;
}


3.在终端输入一个文件名,判断文件的类型


4.字符串倒置:(注意:是倒置,而不是直接倒置输出)

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

void reverseWords(char *s) {
    char *word = strtok(s, " ");
    char *newStr = NULL;
    while (word != NULL) {
        if (newStr == NULL) {
            newStr = strdup(word);
        } else {
            char *oldStr = newStr;
            asprintf(&newStr, "%s\n%s", word, oldStr);
            free(oldStr);
        }
        word = strtok(NULL, " ");
    }
    printf("%s\n", newStr);
    free(newStr);
}

int main() {
    char str[] = "I am Chinese";
    reverseWords(str);
    return 0;
}

相关推荐

  1. 202426

    2024-02-19 23:10:01       28 阅读
  2. 202418学习总结

    2024-02-19 23:10:01       33 阅读
  3. 【Git】切换分支【2024219

    2024-02-19 23:10:01       30 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-02-19 23:10:01       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-02-19 23:10:01       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-02-19 23:10:01       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-02-19 23:10:01       20 阅读

热门阅读

  1. 如何在1Panel上偷渡HTTP/3

    2024-02-19 23:10:01       37 阅读
  2. 代码随想录刷题第34天

    2024-02-19 23:10:01       34 阅读
  3. SQL查询数据是否存在

    2024-02-19 23:10:01       31 阅读
  4. AI技术助力环境问题的解决:潜力与挑战

    2024-02-19 23:10:01       27 阅读
  5. 局部特征描述子与关键点

    2024-02-19 23:10:01       34 阅读
  6. 13.浮动

    2024-02-19 23:10:01       32 阅读