博客
关于我
1009 说反话 (PAT)
阅读量:507 次
发布时间:2019-03-07

本文共 1140 字,大约阅读时间需要 3 分钟。

这里有一个简单的C程序,能够将输入的英语句子中的单词顺序颠倒:

#include 
#include
#include
int main() { char str[100]; int s, p, e, j, i; gets(str); s = strlen(str); p = s; // 分割单词并存储到数组words中 char *words = malloc(p * sizeof(char)); words[0] = '\0'; int word_count = 0; for (i = s - 1; i >= 0; i--) { if (str[i] == ' ') { if (word_count > 0) { words[word_count] = '\0'; word_count++; } j = i + 1; while (j < s && str[j] != ' ') { j++; } for (k = 0; k < j - i - 1; k++) { words[word_count + k] = str[i + 1 + k]; } words[word_count] = '\0'; word_count++; } else { // 非空格字符,直接加入当前单词 if (i > p) { // 展示扩展字符串的处理 // (在实际应用中,应先展开发商提供的动态内存分配方法) } } } if (word_count > 0) { // 输出倒序的单词 printf("%s", words); } free(words); return 0;}

这个程序的工作流程是:

  • 读取输入字符串
  • 逆序遍历字符串,识别并统计单词
  • 将发现的单词倒转排列
  • 输出倒序后的句子
  • 如果需要处理更复杂的文本处理需求,可以考虑使用更专业的文本处理库或工具。

    转载地址:http://ajojz.baihongyu.com/

    你可能感兴趣的文章
    Nginx代理初探
    查看>>
    nginx代理地图服务--离线部署地图服务(地图数据篇.4)
    查看>>
    Nginx代理外网映射
    查看>>
    Nginx代理模式下 log-format 获取客户端真实IP
    查看>>
    Nginx代理静态资源(gis瓦片图片)实现非固定ip的url适配网络环境映射ip下的资源请求解决方案
    查看>>
    Nginx代理静态资源(gis瓦片图片)实现非固定ip的url适配网络环境映射ip下的资源请求解决方案
    查看>>
    Nginx反向代理与正向代理配置
    查看>>
    Nginx反向代理是什么意思?如何配置Nginx反向代理?
    查看>>
    nginx反向代理解决跨域问题,使本地调试更方便
    查看>>
    nginx启动脚本
    查看>>
    Nginx在Windows下载安装启动与配置前后端请求代理
    查看>>
    Nginx多域名,多证书,多服务配置,实用版
    查看>>
    nginx开机启动脚本
    查看>>
    nginx异常:the “ssl“ parameter requires ngx_http_ssl_module in /usr/local/nginx/conf
    查看>>
    nginx总结及使用Docker创建nginx教程
    查看>>
    nginx报错:the “ssl“ parameter requires ngx_http_ssl_module in /usr/local/nginx/conf/nginx.conf:128
    查看>>
    nginx报错:the “ssl“ parameter requires ngx_http_ssl_module in usrlocalnginxconfnginx.conf128
    查看>>
    nginx日志分割并定期删除
    查看>>
    Nginx日志分析系统---ElasticStack(ELK)工作笔记001
    查看>>
    Nginx映射本地json文件,配置解决浏览器跨域问题,提供前端get请求模拟数据
    查看>>