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

【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;
}

 

相关文章:

  • 在WPF程序中设置背景图片
  • 深度解析NL2SQL:从语义理解到工程实践的全链路探索
  • 向量数据库Milvus03-高级功能与性能调优
  • 探索产品经理的MVP:从概念到实践
  • AVL树简介与部分实现
  • 基于pycharm,python,flask,sklearn,orm,mysql,在线深度学习sql语句检测系统
  • Microsoft.ClearScript.V8单例模式封装,方便下次使用。
  • web常见的攻击方式有哪些?如何防御?
  • JVM学习(四)--对象内存布局
  • Vue3性能优化: 大规模列表渲染解决方案
  • CUDA 性能优化 | 共享内存机制 / 向量化访存策略
  • 一个开源的 Blazor 跨平台入门级实战项目
  • Baklib内容中台的主要构成是什么?
  • vscode | Trae【实用插件】Remove empty lines 保存文件时删除空行
  • MQTT-共享订阅
  • 分布式缓存:缓存设计中的 7 大经典问题_缓存失效、缓存穿透、缓存雪崩
  • 解码AI:2025年人工智能技术发展全景图
  • 信息收集与搜索引擎
  • 【案例篇】 实现简单SSM工程-后端
  • 开源轻量级语音合成和语音克隆模型:OuteTTS-1.0-0.6B
  • 没有做网站能备案吗/武汉seo网站排名优化
  • 网站开发架设/外贸平台推广
  • 铜仁 网站开发/关键词调词平台
  • 国外优秀排版设计网站/yoast seo
  • 创业网站怎么做/seo推广哪家公司好
  • 中山网站建设技术/百度网址提交入口