Wednesday, 27 February 2013

Write your own function in C to implement string length and string concatenation without using built in functions.

Function in C to implement string length:

/*function to find the string length*/         
int mystrlen(char str[]) /* int type function, it will accept an array of characters and return total number of characters */
{
            int i;
            for(i=0; str[i] !=’\0’; i++)
            {
             }
return i;
}/*function ends here*/


Function in C to implement string concatenation:

/*function to concatenate two string*/
mystrconcat(char str1[], char str2[])
{
            int i,j;
            i=j=0;
while(str1[i] !=’\0’)
{
            i++;
}
while(str2[j] !=’\0’)
{
str1[i]=str2[j];
            j++;
            i++;
}
str1[i]=’\0’;
}/*function ends here*/

/*in main() function use these function*/
#inclued<stdio.h>
main()
{
char str1[30], str2[30];
printf(“Enter the first string :”);
gets(str1);
printf(“\n Enter the second string :”);
gets(str2);
printf(“Length of first string is : %d and length of second string is : %d \n”, mystrlen(str1), mystrlen(str2));
mystrconcat(str1,str2);
printf(“After concatenation the first string is : %s\n”, str1);
}/*End of main()*/