C Program to find sum of two numbers



#include<stdio.h>
#include<conio.h>
#include<string.h>
int slength(char *);
void main()
{
char string[50];
int length;
clrscr();
printf("\nEnter a string:");
gets(string);
length=slength(string);
printf("Length of string is:%d",length);
getch();
}
int slength(char *str)
{
int length=0;
while(*str !='\0')
{
length++;
str++;
}
return length;
}

Explanation:

The above program will calculate the length of the particular string. First of all we passed the string entered by the user to the function slength by using the pointer. The pointer passed the base address of the string to the function pointer takes the string as an internal array. While loop will calculate the length of string.we can understand the whole process by following example:

string="Hello User"
length= slength(Hello User) (here we are calling the slength function)
This will jump on the definition of slength function with the base address of string i.e *str=str[0]=H
Where length=0
*str=str[0]!='\0',True( '\0' is the termination character of string)
Iteration 1:
Now length=length+1=0+1=1
str++, This statement will move the string to next character as
*str=e
*str=!='\0',True
Iteration 2:
length=1+1=2
str++, This statement will move the string to next character as
*str=l
*str=!='\0',True
Iteration 3:
length=2+1=3
str++, This statement will move the string to next character as
*str=l
*str=!='\0',True
Iteration 4:
length=3+1=4
str++, This statement will move the string to next character as
*str=o
*str=!='\0',True
Iteration 5:
length=4+1=5
str++, This statement will move the string to next character as
*str= " "
*str=!='\0',True
Iteration 6:
length=5+1=6
str++, This statement will move the string to next character as
*str=U
*str=!='\0',True
Iteration 7:
length=6+1=7
str++, This statement will move the string to next character as
*str=s
*str=!='\0',True
Iteration 8:
length=7+1=8
str++, This statement will move the string to next character as
*str=e
*str=!='\0',True
length=8+1=9
str++, This statement will move the string to next character as
*str=e
*str=!='\0',True
length=9+1=10
The output of the above program is given below:

Output:

c program