Login to ask a question from Strings in C programming

Write a C program to find length of a string.

Write a C program to find length of a string.

Without using pointer:
#include 
#define MAX_SIZE 100 // Maximum size of the string

int main()
{
    char text[MAX_SIZE]; /* Declares a string of size 100 */
    int i;
    int count= 0;

    /* Input a string from user */
    printf("Enter any string: ");
    gets(text);

    /* Iterate till the last character of string */
    for(i=0; text[i]!='\0'; i++)
    {
        count++;
    }

    printf("Length of '%s' = %d", text, count);

    return 0;
}

Using pointer:
/**
 * C program to find length of a string using pointer
 */

#include 
#define MAX_SIZE 100 // Maximum size of the string

int main()
{
    char text[MAX_SIZE]; /* Declares a string of size 100 */
    char * str = text; /* Declare pointer that points to text */
    int count = 0;

    /* Input....
Show More
Previous Question Next Question
🔗

Similar Questions

From Strings in C programming · C programming

View All
WhatsApp