so I'm writing a C program that needs to find words (in a text file) that are read the same from beginning AND end. So far, I've written a code that finds out if word's first and last letters are the same but can't really figure out what to do next. Any help, please? :)
#include <stdio.h>
#include <string.h>
const int CMAX = 1000;
const int Dydis = 200;
void algorythm(char *line);
int main(){
void algorythm(char *line){
char word[256];
char rezMasyv[256];
int i=0;
int j=0;
int k=0;
int z=0;
for (i=0;i<strlen(line);i++){
while (line[i]==' '){
line[j]=' ';
++j;
++i;
}
word[z]=line[i];
++z;
if (line[i+1]==' ' || line[i+1]=='\n' || i+1==strlen(line)){
if (word[0]!=word[z-1]){
for (k=0;k<z;++k){
line[j]=word[k];
++j;
}
}
z=0;
}
}
line[j]='\0';
j=0;
}
char duom[CMAX], rez[CMAX], text[Dydis];
FILE *duomFailas;
FILE *rezFailas;
printf("Enter the name of text file \n");
scanf("%s", duom);
duomFailas=fopen(duom, "r");
if (duomFailas==NULL){
printf ("Error opening text file \n");
system("pause");
return 0;
};
printf("Enter the name of result file\n");
scanf ("%s", rez);
rezFailas=fopen(rez, "w");
if (rezFailas==NULL){
printf ("Error opening results file \n");
system("pause");
return 0;
};
while (fgets(text, sizeof(text), duomFailas)) {
algorythm(text);
fprintf(rezFailas,"%s\n",text);
}
fclose(duomFailas);
fclose(rezFailas);
return(0);
}
The text file includes a lot of words seperated by a space ' '. I need to delete the words that are read from beginning and end the same (lol, samas, wololow and etc.).
Thanks for help ;)
You could have two pointers pointing to a word, one pointing to the first letter and another pointing to the last. You then have the two pointers walk towards each other, making sure that they both always point to the same character.
Something like: