C Program that prompts the user to input a string, (whitespaces allowed)

How to Write a program that prompts the user to input a string, (whitespaces allowed)
Add code to:
Step 1: allocate memory for the string
Step 2: Scanf the string from the user
Step 3: Write a function to calculate the length of the string.
            int length(char name[]);Â


Solution:

/*Write a program that prompts the user to input a string, (whitespaces allowed)
Add code to:

step1: allocate memory for the string
step2: Scanf the string from the user
step3: Write a function to calculate the length of the string.
int length(char name[]);Â


*/
#include<stdio.h>
#define SIZE 41

int main()
{ 
 //Define student name
 char name[SIZE];
 printf("Please enter your name:");
 scanf("%[^\n]",name);
 
 printf("The length of the name is %d",length(name));
 getch();
}

int length(char name[])
{
 int i,result=0;
 for(i=0;name[i]!='\0';i++)
  result++;
  
 return result;

}


Learn More :