【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
大于s2
。strncmp
最多比较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;
}