[英]length of string in c language
我寫了這段代碼:
printf("enter a string: ");
scanf("%s",C); // C= "Hello world this is a string"
printf("%d\n",strlen(C));
temp=com0(C);
這段代碼顯示字符串的長度是 5,這也意味着這是第一個單詞的長度,我只需要得到完整的長度,但這不是重點,重要的是我必須將整個字符串傳遞給函數也只打印第一個單詞的長度,它應該打印整個長度
這是函數的代碼:
bool com0(char k[]){
printf("%d\n",strlen(k));
if(k[0]>='a' && k[0] <= 'z'){
return com1(nextchar(k+1));
}
else{
return false;
}
}
它也打印 5 !
這是執行的結果:
使用fgets
而不是scanf
解決了這個問題!
printf("enter a string: ");
fgets(C,sizeof(C),stdin);
C[strcspn(C,"\n")] = 0; // C = "Hello world this is a string"
printf("%d\n",strlen(C)); // print 28
temp=com0(C);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.