Computer Concept & Programming In C - Unit V - 4

Q.10. write a program to store the records in a hotel as customer name, address, period of stay, room allotted and room charges.                                              (AKTU. 2011 - 12)
Ans. In previous program change the fields inside the program i.e. replace name with customer name delete the fields date of birth, phone number and add the following fields
int period_of_stay;
int room_alloted;
float room_charges.
Rest syntax and program is same.

/* Q.11 Write your own 'C' functions to implement the following standard
library string functions
 1.strlen()
 2.strcpy()                            */                                        (AKTU. 2008 - 09)
Q. State some library functions for string manipulation. Also create a function in C to find the length of the string.                                                              (AKTU. 2011 - 12)
Ans.
/* Explanation :
  1.strlen()  accepts an array of character and
   returns the length of the string in the array.

  2.strcpy() accepts the source and destination
  array and copies the string in the source
  array into the destination array               */

#include<stdio.h>
#include<conio.h>
#define size 100

/* defining the function prototypes              */

int strlen(char *string);
void strcpy(char *source, char *destination);

/* global character array to store source
   and destination string                         */

char source[size];
char destination[size];
int length = 0;

void main()
{
 int i;    /* loop counter variable */
 clrscr();

 /* Initializing all elements to '\0' */

 for( i = 0; i < size; i++)
  {
   source[i] = NULL;
   destination[i] = NULL;
  }

printf("\nEnter the string\n->");
gets(source);

/*Printing the length of the string using strlen()      */

printf("\n The length of the source string is:\n");
printf("%d",strlen(source));

/*Copying the source string into the destination array  */
strcpy(source, destination);

printf("\nThe string after being copied into the");
printf("\ndestination is :\n");
puts(destination);
printf("\n Press any key to exit.");
getch();
}
int strlen(char *source)
{
  length = 0;
  do
   {
    length ++;
   }
   while(source[length] != '\0');
   return length;
}
void strcpy(char *source, char *destination)
{
 int i;    /* loop counter variable */
 int len;  /* for storing the length of the string*/
 len = strlen(source);
 for( i = 0; i < len; i++)
    destination[i] = source[i];
}