下一行中的打印行没有使用\ n [英] Printing line in next line whithout use of \n
问题描述
我无法理解为什么语言是不同的行和其他单词在下一行打印即使我没有使用\ n任何地方
我有什么试过:
i couldnot get why language is different line and other words get print in next line even i have not used \n anywhere
What I have tried:
#include<stdio.h>
#include<string.h>
void search_space(char* string,int n);
void print(char *string,int *t,int k);
int main()
{int n;
char string[100];
fgets(string,100,stdin);
n=strlen(string);
search_space(string,n);
}
void search_space(char* string,int n)
{int i=0,j=0,k;
int t[100];
while(string[i]!='\0')
{
if(string[i]==' ')
{t[j]=i;
j++;}
i++;
}
for(k=j-1;k>=0;k--)
print(string,t,k);
}
void print(char *string,int *t,int k)
{
int i=t[k]+1;
while(string[i]!=' '&&string[i]!='\0')
{
printf("%c",string[i]);
i++;
}
}
输出
output
language
programmingbestis
推荐答案
输入字符串时有换行符。请参阅 fgets - C ++参考a> [ ^ ]:
You have a newline character when entering the string. See fgets - C++ Reference[^]:
从流中读取字符并将它们作为C字符串存储到str中,直到读取(num-1)个字符或者到达换行符或文件结尾为止,以先发生者为准。
换行字符使fgets停止读取,但它被函数视为有效字符并包含在复制到str的字符串中。
Reads characters from stream and stores them as a C string into str until (num-1) characters have been read or either a newline or the end-of-file is reached, whichever happens first.
A newline character makes fgets stop reading, but it is considered a valid character by the function and included in the string copied to str.
换句话说:
fgets()
等到你按Enter键转换为换行符并存储在 string
中。仅当您输入100个或更多字符时才会出现换行符,因为 fgets()
在达到限制时返回。
In other words:
fgets()
waits until you press the Enter key which translates to the new line character and that is stored in string
. Only if you enter 100 or more characters there is no newline character because fgets()
returns when reaching the limit.
这篇关于下一行中的打印行没有使用\ n的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!