博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C语言习题:字符串操作函数练习题目
阅读量:2432 次
发布时间:2019-05-10

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

1、将包含字符数字的字符串分开, 使得分开后的字符串前一部分是数字后一部分是字母。例如“h1ell2o3”->”123hello”

#include 
#include
#include
void divide_str(char* str1, char* str2);int main() {
char c[50] = {
0}; char a[50] = {
0}; printf("请输入字符串:\n"); while (gets(c) != NULL) {
divide_str(c, a); } return 0;}void divide_str(char* str1, char* str2) {
if (str1 == NULL || str2 == NULL) {
return; } int len = strlen(str1); int j = 0; for (int i = 0; i < len; i++) {
if (str1[i] >= '0' && str1[i] <= '9') {
str2[j] = str1[i]; j++; } } for (int i = 0; i < len; i++) {
if (isalpha(str1[i])) {
str2[j] = str1[i]; j++; } } printf("转化后的字符串顺序为:\n"); puts(str2);}

2.将字符串中的空格替换成“ % 020”

#include 
void print_str(char* str) {
if (str == NULL) {
return; } int i = 0; int j = 0; char word[50] = {
0 }; while (str[i]) {
if (str[i] == ' ') {
printf("%s", word); printf("%s", "%020"); memset(word, 0, sizeof(word)); j = 0; i++; } else {
word[j] = str[i]; i++; j++; } } puts(word);}int main() {
char str[50] = {
0}; puts("请输入一行字符串:"); while (gets(str) != NULL) {
print_str(str); puts("请再次输入一行字符串:"); } return 0;}

3、删除字符串中指定的字符。例如“abcdaefaghiagkl“删除‘a’, 以后:“bcdefghigkl”

#include 
void delete_char(char* str) {
if (str == NULL) {
return; } char word[50] = {
0}; char a = '\0'; int i = 0, j = 0; puts("please input need to deleted char:"); scanf("%c", &a); while (str[i]) {
if (str[i] != a) {
word[j] = str[i]; j++; } i++; } printf("%s\n", word);}int main() {
char c[50] = {
0}; printf("input str:\n"); while (gets(c) != NULL) {
delete_char(c); printf("input str again:\n"); getchar(); }}

4、删除一个数组中重复的元素。例如 1,2,2,2,3,3,3,4,4,5,5,5,6,6,6 -> 1,2,3,4,5,6

#define _CRT_SECURE_NO_WARNINGS#include 
void delete_num(char* num) {
if (num == NULL) {
return; } char c[50] = {
0 }; int i = 0, j = 0; int len = strlen(num); while(i < len) {
if (num[i] != num[i + 1]) {
c[j] = num[i]; i++; j++; } else {
i++; } } puts(c);}int main() {
char num[50]; while (gets(num) != NULL) {
delete_num(num); } return 0;}

5、将字符串中的相邻的多余空格去掉,例如(空格用下划线表示): __hello____world___how_are_you -> hello_world_how_are_you

#define _CRT_SECURE_NO_WARNINGS#include 
void delete_blank(char* str) {
if (str == NULL) {
return; } int i = 0; int j = 0; char word[50] = {
0 }; while (str[i]) {
if (str[i] == ' ') {
i++; } else if (str[i] != ' '){
word[j] = str[i]; i++; j++; if (str[i] == ' ') {
printf("%s ", word); memset(word, 0, sizeof(word)); j = 0; i++; } } } puts(word);}int main() {
char c[50] = {
0}; while (gets(c) != NULL) {
delete_blank(c); } return 0;}

6、大整数加法,实现任意范围的两个整数的加法( 整数的范围用 int 型的变量无法表示,50位)

#define _CRT_SECURE_NO_WARNINGS#include 
#include
void reserve_str(int* str, int len);void add_big_int(int* a, int* b, int len1, int len2) {
int k = 0, t = 0; int count = 0; //记录计算结果数字的位数 if (len1 > len2) {
//第一个数长度大于第二个数 while (t <= len1 - 1) {
k = a[t] + b[t]; if (k < 10) {
a[t] = a[t] + b[t]; } else {
a[t] = k % 10; a[t + 1] = a[t + 1] + (k - a[t]) / 10; } t++; } if (a[t] != 0 && a[t] > 0) {
count = t + 1; } else {
count = t; } } else {
while (t <= len2 - 1) {
k = a[t] + b[t]; if (k < 10) {
a[t] = a[t] + b[t]; } else {
a[t] = k % 10; a[t + 1] = a[t + 1] + (k - a[t]) / 10; } t++; } if (a[t] != 0 && a[t] > 0) {
count = t + 1; } else {
count = t; } } printf("超大数字A和B的和为:\n"); count--; while (count >= 0) {
printf("%d", a[count]); count--; } printf("\n");}void reserve_str(int* str, int len) {
int start = 0; char c = 0; while (start < len) {
c = str[start]; str[start] = str[len - 1]; str[len - 1] = c; start++; len--; } return;}int main() {
int c[100] = {
0 }; int a[100] = {
0 }; char data; int i = 0, j = 0; printf("请输入一个超大数字A(超过50个字节):\n"); while ((data = getchar()) != '\n'){
c[i++] = data - 48; } reserve_str(c, i); printf("请输入一个超大数字B(超过50个字节):\n"); while ((data = getchar()) != '\n') {
a[j++] = data - 48; } reserve_str(a, j); add_big_int(c, a, i, j); return 0;}

7、求一个字符串数组的最大值和次大值

#include 
#include
void big(char* arr[], int size, char** big1, char** big2);int main() {
char* arr[5] = {
"wuhan", "hello","word","dwdeedef","15646959" }; char* max = arr[0]; //最大字符串和次大字符串的指针 char* c_max = arr[1]; big(arr, 5, &max, &c_max); return 0;}void big(char* arr[], int size, char** big1, char** big2) {
for (int i = 1; i < size; ++i) {
if (strcmp(*big1, arr[i]) < 0) {
//当前字符串大于最大字符串 *big2 = *big1; *big1 = arr[i]; } else if (strcmp(*big2, arr[i]) < 0) {
//当前字符串大于次大字符串 *big2 = arr[i]; } } puts("最大字符串:"); puts(*big1); puts("次大字符串:"); puts(*big2);}

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

你可能感兴趣的文章
malloc的小知识
查看>>
UVALive 6755 - Swyper Keyboard
查看>>
uva_11029 Leading and Trailing 快速幂 数的n次方前几位
查看>>
uva10023 手算开方的方法
查看>>
uva11027
查看>>
欧拉函数——从容斥定理和积性函数的性质谈开
查看>>
容斥原理 带禁止位的排列
查看>>
模拟带Servlet技术的HTTP服务器的Java实现
查看>>
第一个JSP程序(JSP入门)
查看>>
JSP语法简介
查看>>
JavaBean入门与简介
查看>>
JSP中EL表达式入门与简介
查看>>
Spring入门实例
查看>>
Spring的几种注入方式
查看>>
Spring自动装配
查看>>
Hibernate入门与实例
查看>>
Jython入门学习
查看>>
Hiberate基础用法实例
查看>>
Maven编译时指定JDK版本
查看>>
Hibernate单向关联N-1
查看>>