当前位置: 首页 > article >正文

【C语言】习题练手套餐 2

每日习题分享。

字符串函数的运用

首先回顾一下字符串函数。

字符串长度

strlen(const char *s);

功能:计算字符串的长度,不包含终止符\0

字符串连接

char *strcat(char *dest, const char *src);
char *strncat(char *dest, const char *src, size_t n);

功能:将src追加到dest后。strncat最多追加n个字符。

字符串连接

char *strcat(char *dest, const char *src);
char *strncat(char *dest, const char *src, size_t n);

功能:将src追加到dest后。strncat最多追加n个字符。

字符串比较

int strcmp(const char *s1, const char *s2);
int strncmp(const char *s1, const char *s2, size_t n);

功能:比较字符串。返回值为 0 表示相等,<0 表示s1小于s2,>0 表示s1大于s2strncmp最多比较n个字符。

字符串分割

char *strtok(char *str, const char *delim);

功能:将字符串按分隔符delim分割成多个子串。(例如 ,.)

习题一 

 

#include <stdio.h>
#include <string.h>
#define MAXLEN 1000
int main()
{char s[MAXLEN + 1], t[MAXLEN + 1];int len_s, len_t;if(fgets(s, sizeof(s), stdin) != NULL){// 去掉末尾的换行符len_s = strlen(s);if (len_s > 0 && s[len_s - 1] == '\n') {s[len_s - 1] = '\0';len_s--;}}if (fgets(t, sizeof(t), stdin) != NULL){len_t = strlen(t);if (len_t > 0 && t[len_t - 1] == '\n'){t[len_t - 1] = '\0';len_t--;}}// 检查连接后的字符串长度是否超过1000个字符if (len_s + len_t > MAXLEN){printf("错误:连接将导致字符串长度超限。\n");printf("%s\n", s);}else {// 连接字符串 t 到 s 的末尾strcat(s, t);printf("%s\n", s);}return 0;}

本题其实没有任何难度,但是仅仅使用strcat函数的话是不会通过的。需要考虑众多的因素。

例如:
这里把考虑长度的部分删除,直接strcat,输出。

 

习题二

 

#include <stdio.h>
#include <string.h>int main() 
{char s[1001];int pos, len;// 读取字符串(包含空格)if (fgets(s, sizeof(s), stdin) == NULL) {return 1;}// 移除换行符size_t len_s = strlen(s);if (len_s > 0 && s[len_s - 1] == '\n') {s[--len_s] = '\0';}// 读取位置和长度if (scanf("%d %d", &pos, &len) != 2) {return 1;}// 调整为0-based索引pos--;// 检查位置有效性if (pos >= len_s) {printf("%s\n", s);return 0;}// 计算实际删除长度int delete_len = (pos + len > len_s) ? len_s - pos : len;// 移动剩余字符覆盖删除部分memmove(s + pos, s + pos + delete_len, len_s - (pos + delete_len) + 1);// 输出结果printf("%s\n", s);return 0;
}

习题三

首先我们需要考虑下标与位置的关系,由题目可知 6指的是下标5。易错点

其他的在按题目要求来书写。

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>#define MAX_LEN 1000int main() 
{char s[MAX_LEN + 2]; // 主串缓冲区,足够容纳输入和换行符fgets(s, sizeof(s), stdin); // 读取主串// 去除末尾的换行符size_t len_s = strlen(s);if (len_s > 0 && s[len_s - 1] == '\n'){s[len_s - 1] = '\0';len_s--;}int pos, len;scanf("%d %d", &pos, &len); // 题目保证参数合法,无需检查// 直接截取并输出,无需调整int i;
for (i = 0; i < len && (s + pos - 1)[i] != '\0'; i++)
{putchar((s + pos - 1)[i]);
}
putchar('\n');return 0;
}

习题四

 这是一道题的测试点,如果提交后没有通过,需要通过这些来修改代码。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>char* insertString(char* s, char* t, int pos) {int len_s = strlen(s);int len_t = strlen(t);// Check if pos is validif (pos < 1 || pos > len_s + 1) {return NULL;}// Allocate memory for the new stringchar* result = (char*)malloc((len_s + len_t + 1) * sizeof(char));if (result == NULL) {return NULL;}// Copy the first part of s to resultstrncpy(result, s, pos - 1);result[pos - 1] = '\0';// Append t to resultstrcat(result, t);// Append the remaining part of s to resultstrcat(result, s + pos - 1);return result;
}int main() {char s[1000], t[1000];int pos;// Read the input strings and positionfgets(s, sizeof(s), stdin);s[strcspn(s, "\n")] = 0; // Remove newline characterfgets(t, sizeof(t), stdin);t[strcspn(t, "\n")] = 0; // Remove newline characterscanf("%d", &pos);// Insert t into s at position poschar* result = insertString(s, t, pos);if (result == NULL) {printf("错误:指定插入位置不存在。\n");printf("%s\n", s);} else {printf("%s\n", result);free(result);}return 0;
}

 

http://www.lryc.cn/news/2386148.html

相关文章:

  • [项目总结] 基于Docker与Nginx对项目进行部署
  • 神经正切核推导(2)
  • Python模型优化技巧
  • Redis 面试场景
  • MySQL 索引失效及其解决办法
  • Ctrl+鼠标滚动阻止页面放大/缩小
  • 开发积累总结
  • C++虚函数与类对象模型深度解析
  • 3d世界坐标系转屏幕坐标系
  • 【2025】基于Springboot + vue + 协同过滤算法实现的旅游推荐系统
  • AI数据治理破局的战略重构
  • QT6安装与概念介绍
  • Python包管理工具uv 国内源配置
  • ABP VNext + Webhook:订阅与异步回调
  • Docker(二):开机自启动与基础配置、镜像加速器优化与疑难排查指南
  • Lua基础语法
  • 2025年渗透测试面试题总结-匿名[实习]安全工程师(安全厂商)(题目+回答)
  • 【node.js】实战项目
  • 从AD9361 到 ADSY1100 ,中间的迭代产品历史
  • 免费插件集-illustrator插件-Ai插件-查找选中颜色与pantone中匹配颜色
  • redis集合类型
  • [爬虫实战] 爬微博图片:xpath的具体运用
  • MySQL中简单的操作
  • NNG和DDS
  • 防震基座在半导体晶圆制造设备抛光机详细应用案例-江苏泊苏系统集成有限公司
  • 框架开发与原生开发的权衡:React案例分析(原生JavaScript)
  • Lua5.4.2常用API整理记录
  • Python打卡训练营学习记录Day36
  • ### Mac电脑推送文件至Gitee仓库步骤详解
  • 官方SDK停更后的选择:开源维护的Bugly Unity SDK