Computing.Net > Forums > Programming > scanning a word and strcmp in C

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

scanning a word and strcmp in C

Reply to Message Icon

Name: alhaidry
Date: September 15, 2005 at 07:02:06 Pacific
OS: Debian
CPU/Ram: 1g / 380 m
Comment:

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!



Sponsored Link
Ads by Google

Response Number 1
Name: sen (by santanusen_82)
Date: September 15, 2005 at 22:29:35 Pacific
Reply:

/* 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


0

Response Number 2
Name: alhaidry
Date: September 16, 2005 at 07:35:45 Pacific
Reply:

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

AD


0

Response Number 3
Name: sen (by santanusen_82)
Date: September 22, 2005 at 03:43:58 Pacific
Reply:

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


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

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


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: scanning a word and strcmp in C

Help! Test if a number is Prime in C++ www.computing.net/answers/programming/help-test-if-a-number-is-prime-in-c/2794.html

Fullscreen and Color in C++ www.computing.net/answers/programming/fullscreen-and-color-in-c/2442.html

scan to word www.computing.net/answers/programming/scan-to-word/2905.html