C语言中的LTRIM函数

C语言中的LTRIM函数,c,trim,C,Trim,我想用C写一个左微调函数。有人能找出我犯的错误吗 int main() { char string2[]=" wind"; ltrim(string2); int new_len2=strlen(string2); printf("After trim String2 is <%s>\nLength is %d\n",string2,new_len2); return 0; } void ltrim(char *string) { int i=0; wh

我想用C写一个左微调函数。有人能找出我犯的错误吗

int main()
{
  char string2[]="   wind";
  ltrim(string2);
  int new_len2=strlen(string2);
  printf("After trim String2 is <%s>\nLength is %d\n",string2,new_len2);
  return 0;
}

void ltrim(char *string)
{
 int i=0;
 while(string[i]==' ')
   {
    i++;
    string=string+i;
   }
printf("inside function string is <%s>---length is %d\n",string,strlen(string));
}
intmain()
{
char string2[]=“风”;
ltrim(string2);
int new_len2=strlen(string2);
printf(“修剪字符串2后\n长度为%d\n”,字符串2,新的\u len2);
返回0;
}
void ltrim(字符*字符串)
{
int i=0;
while(字符串[i]='')
{
i++;
string=string+i;
}
printf(“内部函数字符串为---长度为%d\n”,字符串,strlen(字符串));
}
输出:

inside function string is <wind>---length is 4
After trim String2 is <   wind>
Length is 7
内部函数字符串为---长度为4
修剪后纵梁2<风>
长度是7

为什么修剪后主函数中的字符串没有变化。

您可以移动字符串的内容,而不是移动指针

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

void ltrim(char *string);

int main()
{
    char string2[]="   wind";
    ltrim(string2);
    int new_len2=strlen(string2);
    printf("After trim String2 is <%s>\nLength is %d\n",string2,new_len2);
    return 0;
}

void ltrim(char *string)
{
    int i=0;
    while(string[0]==' ')
    {
        i = 0;
        while ( string[i]) {//shift the contents
            string[i]=string[i+1];
            i++;
        }
    }
    printf("inside function string is <%s>---length is %d\n",string,strlen(string));
}
#包括
#包括
#包括
void ltrim(字符*字符串);
int main()
{
char string2[]=“风”;
ltrim(string2);
int new_len2=strlen(string2);
printf(“修剪字符串2后\n长度为%d\n”,字符串2,新的\u len2);
返回0;
}
void ltrim(字符*字符串)
{
int i=0;
while(字符串[0]='')
{
i=0;
while(string[i]){//移动内容
字符串[i]=字符串[i+1];
i++;
}
}
printf(“内部函数字符串为---长度为%d\n”,字符串,strlen(字符串));
}

一种有效的方法是计算前导空格的数量,然后将字符串向左移动,一次移动的字符数正好是这个数量:

#include <ctype.h> /* for is blank() */
#include <string.h> /* for memmove() */

void ltrim(char * s)
{
  char * s_tmp = s;

  while (isblank(*s_tmp)) /* isblank() detects spaces and tabs. */
  {
    ++s_tmp;
  }

  memmove(s, s_tmp, s_tmp - s); /* Please note the use of memmove() here, as it
                                   allows the use of overlapping memory areas,
                                   which is not allowed for memcpy().
}
#include/*for is blank()*/
#包括/*用于memmove()*/
无效ltrim(字符*s)
{
char*s_tmp=s;
而(isblank(*s_tmp))/*isblank()检测空格和制表符*/
{
++s_tmp;
}
memmove(s,s_tmp,s_tmp-s);/*请注意此处的memmove()用法,因为
允许使用重叠的内存区域,
这是memcpy()不允许的。
}

不使用


最快的答案似乎是:

char * rtrim(char*);
char * ltrim(char*);

char * rtrim(char * h) {
    size_t s = strlen(h);
    char * string;
    int x = s;
    while (h[x] == ' ' && x >= 0)
    {
       string = string + h[x];
       x--;
    }
    printf("Inside rtrim() function string is <%s>---length is %d\n",string,strlen(string) - x);
    return string;
}

// We, as I had before, do not use sizeof() on strings,
// We have to use strlen because, as you may know
// we need to compensate for different sizes of types
// in certain platforms. Not all are static.
char * ltrim(char * h) {
    size_t s = strlen(h);
    char * string;
    int x = 0;
    while (h[x] == ' ' && x < s)
    {
       string = string + h[x];
       x++;
    }
    printf("Inside ltrim() function string is <%s>---length is %d\n",string,strlen(string) - x);
    return string;
}
char*rtrim(char*);
char*ltrim(char*);
char*rtrim(char*h){
尺寸s=strlen(h);
字符*字符串;
int x=s;
而(h[x]=''&&x>=0)
{
string=string+h[x];
x--;
}
printf(“内部rtrim()函数字符串为---长度为%d\n”,字符串,strlen(字符串)-x);
返回字符串;
}
//正如我之前所说,我们不在字符串上使用sizeof(),
//我们必须使用斯特伦,因为你可能知道
//我们需要补偿不同尺寸的类型
//在某些平台上。并不是所有的都是静态的。
字符*ltrim(字符*h){
尺寸s=strlen(h);
字符*字符串;
int x=0;
而(h[x]=''&&x
提示:在函数运行后检查string2指向何处。从
ltrim
返回
string
,然后从
main
打印它。和
string=string+i
应该是
string++
string=string+1。谢谢,它可以工作,但请您解释一下while(string[i])
的意思是什么,它是否只是检查一个
0
while(string[i])
相当于
while('\0'!=string[i])
。正确,但效率很低,因为工作量呈指数级增长。@user3121023:绝对正确!修复了.1+用于单独处理0字节到副本的情况!:-)这个答案是什么语言?!?!?这是一个C问题,C++,我没有意识到我们只谈论C. @安德烈斯勒。我会更新的。
char * rtrim(char*);
char * ltrim(char*);

char * rtrim(char * h) {
    size_t s = strlen(h);
    char * string;
    int x = s;
    while (h[x] == ' ' && x >= 0)
    {
       string = string + h[x];
       x--;
    }
    printf("Inside rtrim() function string is <%s>---length is %d\n",string,strlen(string) - x);
    return string;
}

// We, as I had before, do not use sizeof() on strings,
// We have to use strlen because, as you may know
// we need to compensate for different sizes of types
// in certain platforms. Not all are static.
char * ltrim(char * h) {
    size_t s = strlen(h);
    char * string;
    int x = 0;
    while (h[x] == ' ' && x < s)
    {
       string = string + h[x];
       x++;
    }
    printf("Inside ltrim() function string is <%s>---length is %d\n",string,strlen(string) - x);
    return string;
}