facebook like button

06 April, 2011

program 26: calculating the length of a string

The need:
     In previous post we came to know that there exists a data-type to handle words and characters. This program takes a string as input and shows position of each character in that string and also counts the number of characters in the string(length of string). Here in this program we come to know a new term 'array-index' or simply 'index'.

The code: 
--------------------------------------------
#define MAX_LENGTH 50
#include<stdio.h>
int main()
{
    int i=0,length;              // declaration of single integer
    char str[MAX_LENGTH];     //declaration of a character string str
    printf("Enter a string\n");
    gets(str);            // getting string and storing in str
    while(str[i]!='\0')
    {
     printf("the index of character %c is %d\n",str[i],i);
     i++;
    }
    length=i;
    printf("\nThe length of given string is %d\n",length);
    return 0;
}

-----------------------------------------

The approach:
     First of all run the program. Read the following explanation and try to understand the code. I hope you remember the use of #define directive(program9).
In C strings are stored as ordered set of characters. So we can say that this particular character is at that number position. Yes. In C we have this built-in facility. So each character can be accessed by its location in that set. This location(position number) is called 'index' of that character in the string. These index start from 0 and continue in increasing sequence. We can access the character at index 'i' by writing str[i] where given string is stored in variable str. One question can arise like, "Up to where the index continue?" The answer is till "the last character of the string". Now second question comes, "How does program comes to know the end of that string?". The answer is in next paragraph.
    When you have to recognize the end of some-thing, you try to find some mark or symbol that signifies its end. This symbol can not occur in the middle of the given thing. e.g. you can tell the end of a sentence by seeing a full-stop after it. So the idea is, there must be something which does not occur in any of the given string so that the program can tell the end of that string. Yes. We have a special character called NULL which is represented as NULL or '\0'. Note here '\0' is a single character literal. This '\0' is placed after the string ends at the time of scanning. Here I also want to tell you that a single character literal is written enclosed in single quotes. e.g. a character literal 'd'. Multi-character literals are written enclosed in double quotes. e.g. a string literal 'ranu'.

   In this program there is a while loop which accesses each character of string str by is index, compare it with NULL and terminates when it is found to be NULL('\0') character. In each run of loop corresponding character gets printed with its index and the index variable is incremented so that in next run next character can be printed. Note that here 'i' is used as index of str in each run of loop. Its obvious that at the end of loop variable 'i' will be having the value of the length of string which is assigned to variable length and printed in next line.

Remark:
    Here in this program I have calculated the length of given string by using a loop to count till the end of string. I further programs you'll see that we have direct function to calculate the length of a string. In fact we have a whole header file to do some things with strings.

No comments:

Post a Comment

feel free to ask your doubts... if any
you can post your doubts on
www.facebook.com/programsimply