Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
I am trying to read a file and look for some words. If I know the work I am searching in advance would it be possible to store it and compare it with what I read from the file using strcmp in c?
for example: if I am reading a c source file and if I see the word void then I print it back as VOID.
so would this work:
char myword[] = "void";
read from the file *f
while (strcmp (myword,*f) != 0)
do cap here.
would this work? or how can I do it in C? Thanks!

/* WARNING: the following program converts all blanks, tabs, newlines to blanks. Make suitable adjustments for that*/
#include<stdio.h>
#include<string.h>main()
{
char *myword = "void";
char word[100];
FILE *src, *dest;src = fopen("source.txt", "r");
dest = fopen("dest.txt", "w");while(fscanf(src, "%s", word) > 0)
{
if(strcmp(word, myword) == 0)
fprintf(dest, "%s ", strupr(word));
else
fprintf(dest, "%s ", word);
}fclose(src);
fclose(dest);remove("source.txt");
rename("dest.txt", "source.txt");
}Santanu Sen
National Institute of Technology
Durgapur
India

Thanks Santanu Sen
the strupr(word) did not work, I am using gcc. is there a lib to include. ThanksAD

Yeah, there is no equivalent function in gcc. why not write the function ourselves?
char *strupr(char *); /*Declaration*/
char *strupr(char *word)
{int i;
for(i = 0; i < strlen(word); i++)
if(word[i] >= 'a' && word[i] <= 'z')
word[i] += ('A' - 'a');return (word);
}
Santanu Sen
National Institute of Technology
Durgapur
India

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |