69,382
社区成员
发帖
与我相关
我的任务
分享
#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;
}
#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;
}
#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[]="helloworld ";
trim(s);
printf("%s!\n",s);
return 0;
}