// 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;
}
C语言实现一个去除字符串中的首尾空格,字母中间有多个空格的去除多余的,剩余一个空格
最新推荐文章于 2024-09-06 22:41:46 发布