-3

I am fairly new to C programming language. And I am getting stuck at finding the size of string the user inputs. A sample code is below:

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 
}

}

So, my problem is I can't find any answers at stack overflow which deals with the size of input above.

Any help will be appreciated.

Iłya Bursov
  • 23,342
  • 4
  • 33
  • 57
Latey Laley
  • 11
  • 1
  • 2
  • you have to allocate buffer enough to receive "any" string, then use strlen to calculate length of actually entered string – Iłya Bursov Mar 30 '18 at 03:39
  • What learning material are you using that suggested `char *input; scanf("%s\n", input);` ? – M.M Mar 30 '18 at 06:12

1 Answers1

0

strlen(char*)

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

The following example shows the usage of strlen() function:

#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);
}

And, if you want find lenght without 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;
}
Community
  • 1
  • 1
Héctor M.
  • 2,302
  • 4
  • 17
  • 35