Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 如何摆脱'\n';在字符串的开头_C_String - Fatal编程技术网

C 如何摆脱'\n';在字符串的开头

C 如何摆脱'\n';在字符串的开头,c,string,C,String,下面是字符串chartemp[]=“\nHello” 如何去掉字符串开头的\n?您可以通过以下方式有效地做到这一点: char* c = "\nnoodle"; if (c[0] == '\n') c++; 您可以使用标准的C函数memmove。比如说 #include <stdio.h> #include <string.h> int main( void ) { char temp[] = "\nHello"; if (temp[0] ==

下面是字符串char
temp[]=“\nHello”


如何去掉字符串开头的
\n
?您可以通过以下方式有效地做到这一点:

char* c = "\nnoodle";
if (c[0] == '\n')
  c++;

您可以使用标准的C函数
memmove
。比如说

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

int main( void )
{
    char temp[] = "\nHello";

    if (temp[0] == '\n')
    {
        memmove(temp, temp + 1, strlen(temp));
    }

    puts(temp);
}
#包括
#包括
内部主(空)
{
字符温度[]=“\nHello”;
如果(临时[0]='\n')
{
memmove(临时,临时+1,strlen(临时));
}
放置(温度);
}
那么

    if (temp[0]=='\n') strcpy(temp, temp+1);

您尝试了什么?您的意思是将其从变量
temp
中删除,还是在没有\n的情况下创建一个新变量?如果您在开始时就知道它…什么阻止了您?在发布此问题之前,由于在研究/解决此问题方面明显缺乏努力而被否决,并且由于在编写此问题时缺乏努力而被否决。是的,帮派向下投票,这是帮助某人学习如何使用S.O.的完美方式。-c'mon…谢谢,但我忘了提到我还想检查字符串开头是否有\n…@Saga如果
如果
,这是一个问题,如果
c
是字符串文本的指针,这很好,但是如果
c
是一个数组(正如OP在posted code中所做的那样),它将不起作用,因为数组是不可修改的左值。
strcpy
通常带有
\u restrict
关键字,这会使您的构造成为一个未定义的行为。只想为后代指出,
memmove()
是这里正确的函数,而不是
memcpy()
strcpy()
(作为另一个尝试的答案),在重叠对象之间复制时会导致未定义的行为。