本文主要是介绍【C/PTA —— 13.指针2(课内实践)】,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
C/PTA —— 13.指针2(课内实践)
- 一.函数题
- 6-1使用函数实现字符串部分复制
- 6-2 拆分实数的整数部分和小数部分
- 6-3 存在感
- 二.编程题
- 7-1 单词反转
一.函数题
6-1使用函数实现字符串部分复制
void strmcpy(char* t, int m, char* s)
{int len = 0;char* ret = t;while (*ret != '\0'){ret++;len++;}if (m > len)*s = '\0';else{t = t + m - 1;while (*t != '\0'){*s = *t;s++;t++;}*s = *t;//还有\0;}
}
6-2 拆分实数的整数部分和小数部分
void splitfloat(float x, int* intpart, float* fracpart)
{int num1 = (int)x;*intpart = num1;float num2 = x-(float)num1;*fracpart = num2;
}
6-3 存在感
int frequency(char* paragraph, char* from, char* to)
{int x = 0;for (int i = 0; paragraph[i] != '\0'; i++){if (paragraph[i] == *from){int flag = 1;for (int j = 0; j < (to - from + 1); j++)if (paragraph[i + j] != *(from + j)){flag = 0;break;}if (flag)x++;}}return x;
}
二.编程题
7-1 单词反转
#include <stdio.h>
#include <string.h>
int main()
{char ch[999];gets(ch);int len = strlen(ch), k = 0, i, j, flag1 = 0, flag2 = 0;ch[len] = ' ';while (k <= len){if (ch[k] != ' ' && flag1 == 0){i = k;flag1 = 1;}if (ch[k] == ' ' && flag2 == 0 && flag1 == 1){flag2 = 1;j = k - 1;}if (flag1 == 1 && flag2 == 1){flag1 = 0;flag2 = 0;for (; j >= i; j--)printf("%c", ch[j]);printf(" ");}k++;}return 0;
}
这篇关于【C/PTA —— 13.指针2(课内实践)】的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!