Hi!
I've tried to split it up a program I made for school into 3:
header.h: has the functions declarations.
func.c: contains the fundctions body.
body.c: uses the functoins.
Here's my code:
/* header.h */
void kelet(char *p);
void copy(char *p1,char *p2);
void insert(char *p1,char *p2);
void reverse(char *p1);
void length(char *p);
void pelet(char *p);
void compare(char *p1,char *p2);
void wordinword(char *p1,char *p2);
void print(char *p1,char *p2);
/* func.c */
#include<stdio.h>
#include<string.h>
#include "header.h"
void kelet(char *p)
{
printf("Please enter string\n");
scanf("%s",p);
}
void copy(char *p1,char *p2)
{
while (*p2)
*p1++=*p2++;
*p1=*p2; // Copies the "/0" at the end
}
void insert(char *p1,char *p2)
{
while (*p2++);
p2--;
while (*p1)
*p2++=*p1++;
*p2=*p1; // Copies the "/0" at the end
}
void reverse(char *p1)
{
char *p2=p1;
int temp,i,len;
len=strlen(p1);
while (*p2++);
p2-=2;
for (i=0;i<len/2;p1++,p2--,i++)
{
temp=*p1;
*p1=*p2;
*p2=temp;
}
}
void length(char *p)
{
int i;
for (i=0;*p++;i++);
printf("The length is %d",i);
}
void wordinword(char *p1,char *p2)
{
char *p3,*p4;
int count=0,len;
len=strlen(p2);
while(*(p1+len-1))
{
p3=p1;
p4=p2;
while(*p4 && *p4==*p3)
{
p4++;
p3++;
}
if (*p4==0)
count++;
p1++;
}
printf("string2 apears %d times in string1",count);
}
void pelet(char *p)
{
printf("%s",p);
}
void compare(char *p1,char *p2)
{
int value=0;
while (*p1==*p2 && *p1 && *p2)
{
p1++;
p2++;
}
value=*p1-*p2;
if (value==0)
printf("String1 == String2");
else if (value<0)
printf("String1 < String2");
else
printf("String2 < String1");
}
void print(char *p1,char *p2)
{
char *p3;
int len,flag=0;
len=strlen(p2);
while (!flag)
{
p3=p2;
while(*p3 && *p3==*p1)
{
p3++;
p1++;
}
if (*p3==0)
{
p1=p1-len;
flag=1;
}
else
p1++;
}
printf("%s",p1);
}
/* body.c */
#include<stdio.h>
#include<conio.h>
#include "header.h"
#define N 100
void main()
{
char string1[N],string2[N];
int choice=0;
clrscr();
printf("Pick your choise from the following menu:\n");
printf("1. Input string1\n2. Input string2\n3. Copy string2 into string1\n4. Insert string1 into string2\n");
printf("5. Reverse string1\n6. Reverse string2\n7. Size of string1\n8. Size of string2\n");
printf("9. Count string2 in string1\n10.Output string1\n11.Output string2\n12.Compare string1 and string2\n");
printf("13.Find string2 in string1 and print the rest of string1\n14.Exit\n");
scanf("%d",&choice);
while (choice!=14)
{
switch(choice)
{
case 1: kelet(string1);
printf("Thank you!");
break;
case 2: kelet(string2);
printf("Thank you!");
break;
case 3: copy(string1,string2);
printf("Done!");
break;
case 4: insert(string1,string2);
printf("Done!");
break;
case 5: reverse(string1);
printf("Done!");
break;
case 6: reverse(string2);
printf("Done!");
break;
case 7: length(string1);
break;
case 8: length(string2);
break;
case 9: wordinword(string1,string2);
break;
case 10:pelet(string1);
break;
case 11:pelet(string2);
break;
case 12:compare(string1,string2);
break;
case 13:print(string1,string2);
break;
default:printf("The number you entered is invalid\n");
}
printf("\nPlease chose another option\n");
scanf("%d",&choice);
}//end of while
printf("Thank you for taking part in our project");
getch();
}
Pay no attention to what the program does.
I use Borland C++ V3.1.
When I try to either link or just Make (F9) one of the .c programs above I get the following error:
Unable to open include file "header.h"
I saved the header as .h format in the same folder as the other 2 files.
What can be the problem?
Thanks
Ami