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

标签 c string sizeof

我对 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/

相关文章:

c - C 编程中可变数量的参数

java - 在哪里验证字符串参数

html - 删除(不间断的)字符串中的空格字符

C语言中有符号和无符号整数的比较

C++ fread : unsigned char and short mixture

c - C 释放内存时出错

c - 如何在C中计算函数参数的大小?

我可以通过 C 宏自动收集函数列表吗

java - 为字符串中的每个字符分配一组新字符

c - 像访问单个数组一样访问结构成员?