关于字符串截断的问题

chenzq87 2011-12-01 08:44:18
一个字符串的问题请教大家:要求写一个函数,将一个字符串里面的最后面的空格去掉,比如“helloworld”,world后面有两个空格,函数原型:void strcut(char *str)
...全文
276 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 wcg_jishuo 的回复:]

记录空格开始处位置,把该处置设置为0即可
如果是字符串中间有空格呢?
[/Quote]
楼主的要求是最后一个,中间的Trim()也没这功能
AIGPTchina 2011-12-02
  • 打赏
  • 举报
回复
记录空格开始处位置,把该处置设置为0即可
如果是字符串中间有空格呢?
  • 打赏
  • 举报
回复
记录空格开始处位置,把该处置设置为0即可
  • 打赏
  • 举报
回复

#include<iostream>
#include<string>
using namespace std;

void strcut(string &str)
{
string::iterator itr = str.begin();

while(*itr++);
itr -= 2;

while(' ' == *itr)
{
--itr;
}

*++itr = '\0';

cout<<str<<endl;
}

int main()
{
string str("hello world ");
strcut(str);
return 0;
}
vilnies 2011-12-01
  • 打赏
  • 举报
回复
char *string="hello world ";

改为
char string[]="hello world ";

"hello world "是字符串常量,不能修改


chenzq87 2011-12-01
  • 打赏
  • 举报
回复
这个是可行的,谢谢你,但是如果我在调用函数的时候直接给他一个字符串指针,就会导致段错误,为什么呢?这个一直困扰我,代码如下:
[code=C/C++][#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void strcut(char *str)
{
if (str == NULL)
{
return;
}

char *str_start = str;

while (*str != '\0') ++ str;
while (--str >= str_start && *str == ' ');

*(str + 1) = '\0';
}

int main()
{
char *string="hello world ";

strcut(string);
printf("%s",string);
printf("#\n"); //used for testing

return 0;
}]
[Quote=引用 3 楼 qq120848369 的回复:]

C/C++ code
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void strcut(char *str)
{
if (str == NULL)
{
return;
}

char *str_start = str;

while (*str != '\0')……
[/Quote]
chenzq87 2011-12-01
  • 打赏
  • 举报
回复
这个是可行的,谢谢你,但是如果我在调用函数的时候直接给他一个字符串指针,就会导致段错误,为什么呢?这个一直困扰我,代码如下:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void strcut(char *str)
{
if (str == NULL)
{
return;
}

char *str_start = str;

while (*str != '\0') ++ str;
while (--str >= str_start && *str == ' ');

*(str + 1) = '\0';
}

int main()
{
char *string="hello world ";

strcut(string);
printf("%s",string);
printf("#\n"); //used for testing

return 0;
}


[Quote=引用 3 楼 qq120848369 的回复:]

C/C++ code
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void strcut(char *str)
{
if (str == NULL)
{
return;
}

char *str_start = str;

while (*str != '\0')……
[/Quote]
AIGPTchina 2011-12-01
  • 打赏
  • 举报
回复
#include<string.h>
#include <stdio.h>
void trimspace(char *);
main()
{
char str[100];
gets(str);
trimspace(str);
//puts(str);
printf("处理后:\n[%s]\n", str );
}
/*------------Found Mistake Below------------*/
void trimspace(char *p)
{
char *q;
if(*p!='\0')
q=p+strlen(p)-1;
else
return;
/*------------Found Mistake Below------------*/
for(;*q==' ';q--)
*q='\0';
for(q=p;*q==' ';q++);
strcpy(p,q);
return;
}
AnYidan 2011-12-01
  • 打赏
  • 举报
回复
这个不是调用库函数,而是库函数中实现trim的源码

就顶这句
柯本 2011-12-01
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 chenzq87 的回复:]

请问能不能不用函数库,而只是通过指针操作来实现呢?

引用 1 楼 keiy 的回复:

busybox中库函数的实现,供参考:
C/C++ code

#include <stdio.h>
#include <string.h>
#include <ctype.h>

void trim(char *s)
{
size_t len = strlen(s);
size……
[/Quote]
这个不是调用库函数,而是库函数中实现trim的源码
qq120848369 2011-12-01
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void strcut(char *str)
{
if (str == NULL)
{
return;
}

char *str_start = str;

while (*str != '\0') ++ str;
while (--str >= str_start && *str == ' ');

*(str + 1) = '\0';
}

int main()
{
char buffer[100];

while (fgets(buffer, 100, stdin) != NULL)
{
buffer[strlen(buffer) - 1] = '\0'; //erase \n
strcut(buffer);
printf(buffer);
printf("#\n"); //used for testing
}

return 0;
}

chenzq87 2011-12-01
  • 打赏
  • 举报
回复
请问能不能不用函数库,而只是通过指针操作来实现呢?

[Quote=引用 1 楼 keiy 的回复:]

busybox中库函数的实现,供参考:
C/C++ code

#include <stdio.h>
#include <string.h>
#include <ctype.h>

void trim(char *s)
{
size_t len = strlen(s);
size_t lws;

/* trim trailing whitesp……
[/Quote]
柯本 2011-12-01
  • 打赏
  • 举报
回复
busybox中库函数的实现,供参考:

#include <stdio.h>
#include <string.h>
#include <ctype.h>

void trim(char *s)
{
size_t len = strlen(s);
size_t lws;

/* trim trailing whitespace */
while (len && isspace(s[len-1])) --len;

/* trim leading whitespace */
if(len) {
lws = strspn(s, " \n\r\t\v");
memmove(s, s + lws, len -= lws);
}
s[len] = 0;
}

int main()
{
char s[]="helloworld ";

trim(s);
printf("%s!\n",s);
return 0;
}

69,382

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧