user-0 提问: 写一个算法，怎样将字符串前后的0去掉

回复:
user: user-1
content: char buffer[] = "0000123123123000000"; int length = strlen(buffer); char * p = buffer; char * q = buffer + length; while (*p == '0') p++; while (*(q - 1) == '0') q--; *q = 0; char result[128]; strcpy(result, p);

user: user-2
content: 引用 5 楼 yaojuntong77 的回复:首部设置一个指针，尾部设置一个指针，两头往中间扫描，然后就获取到中间要的那段了，其余的楼主自己拷贝，嘻嘻  每天回帖即可获得10分可用分！   #include <stdio.h> #include <string.h>  char *StrTrimChar(char * const cstr, const size_t strlength, const char ctrim) {     char  *head = cstr;     char  *tail = cstr + strlength - 1;      if (NULL == cstr)     {         return cstr;     }      while (*head == ctrim && '\0' != *head)     {         head ++;     }     if ('\0' == *head)     {         cstr[0] = '\0';         return cstr;     }      while (*tail == ctrim && head != tail)     {         tail --;     }     *(++tail) = '\0';      return head; }  int main (void) {     char char_to_trim = '0';     char str1_to_trim[] = "0000123123123000000";     char str2_to_trim[] = "00werwe000000000";     char str3_to_trim[] = "qwe000asdasd000";      printf("%s\n", StrTrimChar(str1_to_trim, strlen(str1_to_trim), char_to_trim));     printf("%s\n", StrTrimChar(str2_to_trim, strlen(str2_to_trim), char_to_trim));     printf("%s\n", StrTrimChar(str3_to_trim, strlen(str3_to_trim), char_to_trim));      return 0; }   ------------------------------------------------------------------------------------------------------------------------------------------------------------------ 1>------ 已启动生成: 项目: exam006, 配置: Debug Win32 ------ 1>正在编译... 1>testmain.c 1>正在链接... 1>正在嵌入清单... 1>生成时间 0:00 1>生成日志保存在“*****************************************************************************” 1>exam006 - 0 个错误，0 个警告 ========== 生成: 1 已成功, 0 已失败, 0 最新, 0 已跳过 ==========  ------------------------------------------------------------------------------------------------------------------------------------------------------------------ 123123123 werwe qwe000asdasd 请按任意键继续. . .

user: user-3
content: 引用 2 楼 qq_24484465 的回复:引用 1 楼 u011673029 的回复:你把他们保存到string里面，之后从尾部查找0，找到了就删除，遇到不是的就停止，从头也是一样的中间的不用删除 对的，按照这个逻辑中间是不删除的

user: user-4
content: #include <stdio.h> #include <stdlib.h>  char *StrTrimChar(char *cstr, char ctrim) {     char *dest = cstr;      char *begin = cstr;     while (*begin == ctrim) {         begin++;     }      char *end = begin;     while (*end != 0) {         end++;     }     end--;      while (*end == ctrim) {         *end-- = 0;     }     end++;      while (begin < end) {         *cstr++ = *begin++;     }     *begin = 0;      return dest; }  int main(int argc, char *argv[]) {     char char_to_trim = '0';     char str1_to_trim[] = "0000123123123000000";     char str2_to_trim[] = "00werwe000000000";     char str3_to_trim[] = "qwe000asdasd000";      printf("%s\n", StrTrimChar(str1_to_trim, char_to_trim)); // 1231231233123     printf("%s\n", StrTrimChar(str2_to_trim, char_to_trim)); // werwewe     printf("%s\n", StrTrimChar(str3_to_trim, char_to_trim)); // qwe000asdasd      return 0; }

user: user-5
content: #include "boost/algorithm/string.hpp" #include <iostream> int main() { 	 	std::string s = "0000123123123000000"; 	boost::trim_if(s, boost::is_any_of("0"));  	std::cout << s; 	system("pause");  	return 0; }

user: user-6
content: #include <stdio.h> #include <string.h> char *trimlr0(char *s) {     int h,t;     h=0;     while (1) {         if ('0'!=s[h]) break;         h++;     }     t=strlen(s)-1;     while (1) {         if (t<0||'0'!=s[t]) break;         t--;     }     if (t>0) s[t+1]=0;     return s+h; } int main() {     char s1[]="0000123123123000000";     char s2[]="00werwe000000000";     char s3[]="qwe000asdasd000";     char s4[]="0";     char s5[]="";      printf("[%s]\n",trimlr0(s1));     printf("[%s]\n",trimlr0(s2));     printf("[%s]\n",trimlr0(s3));     printf("[%s]\n",trimlr0(s4));     printf("[%s]\n",trimlr0(s5));      return 0; } //[123123123] //[werwe] //[qwe000asdasd] //[] //[] //

user: user-0
content: 引用 1 楼 u011673029 的回复:你把他们保存到string里面，之后从尾部查找0，找到了就删除，遇到不是的就停止，从头也是一样的中间的不用删除

user: user-3
content: 你把他们保存到string里面，之后从尾部查找0，找到了就删除，遇到不是的就停止，从头也是一样的

user: user-7
content: 从头尾各找到第一个不是0的为止

user: user-8
content: 组合一下就行了     string s="qwe000asdasd000";     auto st=s.find_first_not_of('0');     auto s1 =s.substr(st,s.find_last_not_of('0')-st+1);

user: user-2
content: 首部设置一个指针，尾部设置一个指针，两头往中间扫描，然后就获取到中间要的那段了，其余的楼主自己拷贝，嘻嘻  每天回帖即可获得10分可用分！