EmbLogic's Blog

c program to find length of two string using function,pointer and memory allocation technique

#include
#include
char *input();
int length(char *);
int main()
{
char *s1,*s2;
int i;
printf(“Enter string 1:”);
s1=input();
printf(“Enter string 2:”);
s2=input();
i=length(s1);
printf(“Length of string is:%d\n”,i);
}
char *input()
{
char *s,ch;
int i=0;
s=(char *)malloc(sizeof(char));
while(1)
{
scanf(“%c”,&ch);
s=realloc(s,(i+2)*sizeof(char));
if(ch==’.’)
break;
s[i]=ch;
i++;
}
return s;
}
int length(char *s1)
{
int i=0,count=0;
while(*(s1+i)!=”)
{
count++;
i++;
}
return count;
}

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>