#include <iostream> using namespace std; #include <string.h> #include <stdio.h> /*去掉右边的空格*/ char* rtrim(char* str) { int len = 0; int i = 0; len = strlen(str); for( i = len; i > 0; i--) { if( *(str+(i-1)) == ' ' ) *(str+(i-1)) = '\0'; else break; } return str; } /*去掉左边的空格*/ char* ltrim(char* str) { int len = 0; int i = 0; len = strlen(str); for(i = 0; i < len; i++) { if( *(str+i) != ' ' ) break; } return str+i; } /*去掉左右空格*/ char* trim(char* str) { return ltrim(rtrim(str)); } int main() { char str[100] ; strcpy(str," str "); strcpy(str,trim(str)); std::cout<<str<<std::endl; system("PAUSE"); return 0; }
标签:char,int,len,空格,str,字符串,去掉 From: https://www.cnblogs.com/hshy/p/18335602