Exercise 8

Write a C program to concatenate two strings without using built in functions.

SOURCE CODE:

/*Program to concatenate two strings without using string library functions.*/
#include<stdio.h>
#include<conio.h>
void main()
{
 char str1[50],str2[20];
 int i,j,len=0;
 clrscr();
 printf("\nEnter String1:\n");
 gets(str1);
 printf("\nEnter String2:\n");
 gets(str2);
 for(i=0;str1[i]!='\0';i++)
  len++;
 for(j=0;str2[j]!='\0';j++,i++)
  str1[i]=str2[j];
 str1[i]='\0';
 printf("\nConcatenated string is:\n");
 puts(str1);
}

OUTPUT:

 

Write a C program to compare two strings without using built in functions.

SOURCE CODE:

/*Program to compare two strings.*/
#include<stdio.h>
#include<conio.h>
void main()
{
 char str1[30],str2[30];
 int i,flag=0;
 clrscr();
 printf("Enter String1:\n");
 gets(str1);
 printf("Enter String2:\n");
 gets(str2);
 for(i=0;str1[i]!='\0'&&str2[i]!='\0';i++)
 {
  if(str1[i]!=str2[i])
  {
   flag=1;
   break;
  }
 }
 if(flag==1)
  printf("Strings are not same.");
 else
  printf("String1 is same as String2.");
}

OUTPUT: