C语言实现一个去除字符串中的首尾空格,字母中间有多个空格的去除多余的,剩余一个空格

来源:互联网 发布:mysql存储过程递归 编辑:程序博客网 时间:2024/06/10 11:05
// meituanmoni.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include <iostream>#include <stdio.h>using namespace std;int _tmain(int argc, _TCHAR* argv[]){//char *sourceString = "   liu   shuan peng   ";char *sourceString = "   liu   shuan peng";int len = strlen(sourceString);//char *str = new char[len+1];char *str = (char *)malloc(len+1);//char str[100] = {0};int count = 0;int j = 0;int m = 0;for(int i=0;i<len;i++){if(sourceString[i] != ' ' && count == 0){str[j++] = sourceString[i];count = 1;m=1;}if(sourceString[i] == ' ' && count == 1){str[j++] = sourceString[i];count = 0;}if(sourceString[i] != ' ' && count == 1 ){count = 0;}if(sourceString[i] == ' ' && count == 0 && m == 1){str[j++] = sourceString[i];count = 0;m=0;}str[j] = '\0';}if (str[j - 1] == ' '){str[j - 1] = '\0';}cout << strlen(str) << endl;for (int i=0;i < strlen(str);i++){cout << str[i] << endl;}cout << strlen(str) << endl;return 0;}

0 0
原创粉丝点击