gpt4 book ai didi

c - 用户在运行时输入的字符串的大小

转载 作者:行者123 更新时间:2023-11-30 21:06:18 25 4
gpt4 key购买 nike

我对 C 编程语言相当陌生。我一直在寻找用户输入的字符串的大小。示例代码如下:

char * input;
int main(){

printf("Enter the string : ");
scanf("%s\n", input);

int n = ( ) // this is where i put the size of input which i need at run time for my code to work

for (int i=0; i<n; i++){
// code here
// i create threads here
}
for (int i=0; i<n; i++){
// code here
// i join threads here
}

}

所以,我的问题是我在堆栈溢出中找不到任何处理上述输入大小的答案。

任何帮助将不胜感激。

最佳答案

strlen(char*)

In C, this is the function to find the length of a string

以下示例显示 strlen() 函数的用法:

#include <stdio.h>
#include <string.h>

int main () {
char str[50];
int len;

strcpy(str, "LOL");

len = strlen(str);
printf("Length of |%s| is |%d|\n", str, len);

return(0);
}

并且,如果您想在不使用 strlen() 的情况下查找长度

#include <stdio.h>

int main()
{
char s[1000], i;

printf("Enter a string: ");
scanf("%s", s);

for(i = 0; s[i] != '\0'; ++i);

printf("Length of string: %d", i);
return 0;
}

关于c - 用户在运行时输入的字符串的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49567823/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com