Originally Posted by
Adak
It would help if this was in a separate function, so as not to further complicate your main function.
I have made the function as you advised. The entire code is here:
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
/* function to find the first and last letter in the word */
void find_letter (char* a, char* b, char* c, char* d, char* e)
{
char string = strlen(b);
strncpy (a,b,1);
a[1]='\0';
c = strpbrk (a, d);
e= strpbrk (c, d);
printf("%s\n", e);
}
//The pattern
//strncpy (str2,pch,1);
// str2[1]='\0';
// v = strpbrk (str2, vowels);
// j = strpbrk (v,vowels);
// printf("%s\n%s\n",pch, j);
//find_letter(str2, pch, l, letters, j);
/* function to reverse the letters in the word */
char* rev(char* str)
{
int end= strlen(str)-1;
int start = 0;
while( start<end )
{
str[start] ^= str[end];
str[end] ^= str[start];
str[start]^= str[end];
++start;
--end;
}
return str;
}
/* function so I don't have to type getch all over the place */
void MyExit(void) { system("pause"); }
/* the main program */
int main ()
{
/* declaring and initiaizing variables */
char alpha[] = "101";
char beta[] = "100";
char charlie[] = "001";
char vowels[] = "aeiouy";
char letters[] = "bcdfghjklmnpqrstvwxz";
char str2[7];
int echo;
int delta;
int llamba;
int total = 0;
int tab = 0;
char *b = malloc(sizeof(char)*1000);
char *filedata_2 = malloc(sizeof(char)*1000);
char * pch = malloc(sizeof(char)*1000);
char *v = malloc(sizeof(char)*1000);
char *j = malloc(sizeof(char)*1000);
char *k = malloc(sizeof(char)*1000);
char *l = malloc(sizeof(char)*1000);
char *r = malloc(sizeof(char)*1000);
int one, *A;
int two, *B;
int three, *C;
FILE *sp;
atexit(MyExit);
A = &one;
B = &two;
C = &three;
*A = 0;
*B = 0;
*C = 0;
sp = fopen("worklist_2.txt", "r");
echo = atoi (alpha);
delta = atoi (beta);
llamba = atoi (charlie);
if(!sp)
{
perror("Error: file worklist_2.txt was not found or opened");
return 0;
}
/* input sentence */
printf("Input a short sentence: ");
gets(b);
pch = strtok (b, " ,.-");
while(pch != NULL)
{
/* compare input word to word in text file */
while(fgets(filedata_2, 300, sp))
{
if(_memicmp(pch, filedata_2, strlen(pch)) == 0
&& (filedata_2[strlen(pch)] == ' '
|| filedata_2[strlen(pch)] == '\n'))
{
tab = 1;
break;
}
else
{
strcpy(filedata_2, pch);
strcat(filedata_2, " No match!\n");
}
}
/* identify the first and last letter in the word */
find_letter(str2, pch, l, letters, j);
rev(pch);
find_letter(str2, pch, l, letters, j);
/* necessary stuff but not what the program is about */
rewind(sp);
pch = strtok (NULL, " ,.-");
}
return 0;
}
Here is the results:
Code:
Input a short sentence: good stuff stan
g
d
s
f
s
n
Press any key to continue . . .
I can change
Code:
find_letter(str2, pch, l, letters, j);
rev(pch);
find_letter(str2, pch, l, letters, j);
to
Code:
find_letter(str2, pch, l, vowels, j);
rev(pch);
find_letter(str2, pch, l, vowels, j);
But I can't do this:
Code:
find_letter(str2, pch, l, letters, j);
find_letter(str2, pch, l, vowels, j);
rev(pch);
find_letter(str2, pch, l, letters, j);
find_letter(str2, pch, l, vowels, j);
Can you help me fix this problem, Adak?