代码1
来源:互联网 发布:工业控制软件有哪些 编辑:程序博客网 时间:2024/05/16 12:13
#include <stdio.h>#include <string.h>#include <stdlib.h>#include <ctype.h>#include <assert.h>#include "mystack.h"#define MAX_LINE 256#define ARRAY_SIZE 32//======================================================// 去掉单词间多余的空格//======================================================int Handleblank(char *strInput, char *strOutput){char *p = strInput;bool bIsBlank = false;bool bIsWord = false;int num = 0;int i = 0;int j = 0;for (i = 0; i < strlen(strInput); i++){if (strInput[i] == ' ') {if (!bIsBlank) {bIsBlank = true;strOutput[j++] = strInput[i];} bIsWord = false;} else {bIsBlank = false;strOutput[j++] = strInput[i];if (!bIsWord) {bIsWord = true;num++;}}}strOutput[j] = '\0';return num;}//======================================================// 将字符串中的单词进行分离//======================================================int SeparateWords(char *strInput, char *strptr[]){assert(strInput != NULL);assert(strptr != NULL);char word[MAX_LINE] = {0};int i = 0;int j = 0;int k = 0;bool bIsWord = false;while (strInput[i] != '\0'){if (isalnum(strInput[i])) {bIsWord = true;word[j++] = strInput[i];} else if (bIsWord) {bIsWord = false;word[j] = '\0';strptr[k] = (char *)malloc(sizeof(char) * (j + 1));strncpy(strptr[k++], word, j + 1);memset(word, 0, j + 1);j = 0;}i++;}if (j != 0)// 最后一个{word[j] = '\0'; strptr[k] = (char *)malloc(sizeof(char) * (j + 1));strncpy(strptr[k++], word, j + 1);}return k;}//======================================================// 将字符串中的单词进行分离//======================================================int SeparateWords2(char *strInput, char *strptr[]){assert(strInput != NULL);assert(strptr != NULL);char word[MAX_LINE] = {0};int i = 0;int j = 0;int k = 0;bool bIsWord = false;do {if (isalnum(strInput[i])) {bIsWord = true;word[j++] = strInput[i];} else if (bIsWord) {bIsWord = false;word[j] = '\0';strptr[k] = (char *)malloc(sizeof(char) * (j + 1));strncpy(strptr[k++], word, j + 1);memset(word, 0, j + 1);j = 0;}i++;} while (strInput[i] != '\0');if (j != 0)// 最后一个{word[j] = '\0'; strptr[k] = (char *)malloc(sizeof(char) * (j + 1));strncpy(strptr[k++], word, j + 1);}return k;}
<pre class="cpp" name="code">//========================================================// 该函数将输入的数值颠倒输出且不含相同数字// 输入:82473413; 输出:314728//========================================================unsigned int HandleValue(unsigned int val){char strInt[10] = {0};char strInput[10] = {0};char strOutput[10] = {0};int i = 0;int j = 0;sprintf(strInput, "%d", val);for (i = strlen(strInput) - 1; i >= 0; i--) {if (strInt[strInput[i] - '0'] == 1) {continue;} else {strInt[strInput[i] - '0'] = 1;strOutput[j++] = strInput[i];}}strOutput[j] = '\0';return atoi(strOutput);}unsigned int HandleValue2(unsigned int val){char strInt[10] = {0};char strOutput[10] = {0};int i = 0;do {int tmp = val % 10;if (strInt[tmp] == 0) {strInt[tmp] = 1;strOutput[i++] = tmp + '0';}} while (val /= 10);return atoi(strOutput);}
int main(){FILE *fp1;FILE *fp2;char strInput[MAX_LINE] = " Hello, Life is what you can make it!, fighting! ";char strInput2[MAX_LINE] = "Things can only get better";char strOutput[MAX_LINE] = {0};int val = Handleblank(strInput, strOutput);printf("%d\n", val);printf("%s\n", strInput);printf("%s\n", strOutput);char *strPtr[ARRAY_SIZE] = {NULL};val = SeparateWords(strInput, strPtr);printf("%d\n", val);for (int i = 0; i < val; i++) {printf("%s\n", strPtr[i]);}for (int k = 0; k < val; k++) {free(strPtr[k]);strPtr[k] = NULL;}printf("%s\n", strInput2);val = SeparateWords(strInput2, strPtr);printf("%d\n", val);for (i = 0; i < val; i++) {printf("%s\n", strPtr[i]);}for (k = 0; k < val; k++) {free(strPtr[k]);strPtr[k] = NULL;}//fclose(fp1);//fclose(fp2);return 0;}
0 0
- 代码1
- 代码1
- 代码1
- 代码1
- 代码1
- 代码 1
- 代码1
- 代码1
- 提高代码可维护性(1)---代码注释
- 2015061504 - 代码分析之代码格式(1)
- 代码1-1
- 常用代码1
- 常见问题及代码(1)
- 代码管理(1)
- 数据结构代码整理(1)
- asp常用代码1
- .net常用代码1
- ASP代码优化1
- 2014_11_5--2014_11_13(相信自己,梦不再遥远)
- java基础知识回顾
- R语言与数据挖掘学习笔记(常用的包)
- 变分推断(variational inference)学习笔记(1)——概念介绍
- epoll - socket-server-client
- 代码1
- js实践篇:例外处理Try{}catch(e){}
- __toString()方法
- 【BZOJ】【P3226】【Sdoi2008】【校门外的区间】【题解】【线段树】
- 程序猿看哭《奋斗》的米莱,not《粪斗》——not《shit fight》
- Mac OS X Git安装教程
- Android获取除状态栏高度
- 常用连续型分布介绍及R语言实现
- git报错 unable to access 'https://****': SSL certificate problem: Invalid