[英]how to scan strings with white spaces and print them C
我必須讀取一個字符串,與另一個字符串連接並打印我試過這個代碼的結果:
int main(){
char s= "StackOverflow ";
char ss[100];
fgets(ss,100,stdin);
// i know i can use strcat but i don't want it here
printf("%s%s",s,ss);
return 0;
}
ss是“最好的學習東西的網站!” 一些幫助?
首先,您必須使您的程序可編譯,因為編譯會產生一些錯誤。
1.) s
必須定義為char *s
或char s[]
因為char s
只允許在那里存儲一個字符,而不是數組。 char *s
聲明一個指向字符串文字的指針, char s[]
聲明一個與字符串文字中存在的字符一樣多的字符數組,並使用指定字符串文字的字符對其進行初始化。
2.) stdin
是一個FILE *
類型的全局變量(實際上它不是,但它的行為是)代表標准輸入。 如果不聲明它,編譯器不知道stdin
標識符來自哪里,因此會發出錯誤。 消除第二個錯誤的方法是#include <stdio.h>
,它有一個定義並讓編譯器滿意。
完成這兩個步驟后,您的程序是:
#include <stdio.h>
int main(){
char s[]= "StackOverflow ";
char ss[100];
fgets(ss,100,stdin);
// i know i can use strcat but i don't want it here
printf("%s%s",s,ss);
return 0;
}
並按照您的意願行事。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.