这是我的代码:
#include #include int string_length(char s[]); main() { char s[50]; int l; printf("Enter the stringn"); scanf("%sn",s); l=strlen(s); printf("Length of string = %dn",l); } int string_length(char s[]) { int i; i=0; while(s[i] != ' ') ++i; return i; }
编译后,扫描两个输入值。
我的代码出了什么问题?
摆脱scanf
中的换行符。
scanf("%s",s);
这应该让这个代码工作。
但是如果你不得不使用strlen()
我无法理解为什么你编写了一个计算字符串长度的函数。
HTH,
斯利拉姆。
你在main
调用strlen
而不是你自己的string_length
。
const size_t sillyStrlen(const char* text) { if (*text) { return sillyStrlen(text + 1) + 1; } return 0; }
试试这个。
的#include中 的#include 的#include 的 int main() { char * str; printf(“输入字符串 n”); 得到(STR); printf(“字符串的大小是%d”,strlen(str)); 的getch(); 返回0; }
以上就是c/c++开发分享一个程序来查找字符串的长度相关内容,想了解更多C/C++开发(异常处理)及C/C++游戏开发关注(猴子技术宅)。
”本文来自网络收集,不代表猴子技术宅立场,如涉及侵权请点击右边联系管理员删除。
如若转载,请注明出处:https://www.ssfiction.com/c-cyuyankaifa/545978.html