简单的问题(我认为),在C中,我能够使用以下方法扫描整行:
fgets(line, MAX, input); printf("%s\n", line);例如,它会打印出“请把我切成两半”,我怎么只能把“我变成一半”,包括白色空格。
Simple question (I think), in C, I was able to scan in an entire line using:
fgets(line, MAX, input); printf("%s\n", line);Where it would, for example, print "Please cut me in half", how do I only get "me in half", including white spaces.
最满意答案
在扫描整条线之前,您不知道中间位置。 但你可以扫描整行,然后只打印下半部分,如下所示:
printf("%s\n", line+strlen(line)/2);这就是上面代码的工作方式: strlen确定整个字符串的长度(21),然后我们用整数除法(10)将它除以一半,将它添加到指向行开头的指针,并将结果传递给printf 。
You do not know where the middle is until you scan the whole line. But you can scan the entire line, and then print only the second half, like this:
printf("%s\n", line+strlen(line)/2);This is how the above code works: strlen determines the length of the entire string (21), then we divide it in half using integer division (10), add it to the pointer to the beginning of the line, and pass the result to printf.
更多推荐
发布评论