Help with strings and parsing
Author |
Message |
j #1 / 8
|
 Help with strings and parsing
for the following code, how would I cut out all non alpha characters, but leave apostrophys in the string ch? This language is confusing as hell, guess I've gotten used to perl for too long :) Thanks,
int main(int argc, char *argv[]) { FILE *infilep; int num=0; char ch[20]; if (infilep == NULL) { printf("cannot open %s for input\n", argv[1]); } infilep = fopen(argv[1], "r"); while ((fscanf(infilep, "%s", &ch)) != EOF) { ch == strlwr(ch); /*converts string to lower case*/ printf("%d %s\n", num,ch); ++num; } fclose(infilep); return(0); Quote: }
|
Fri, 07 Dec 2001 03:00:00 GMT |
|
 |
b.vaningensche.. #2 / 8
|
 Help with strings and parsing
Quote:
>for the following code, how would I cut out all non alpha characters, >but leave apostrophys in the string ch?
The code below does not process the strings, so I can't say anything on that. There are some other problems with the code, which I will correct. Quote: >This language is confusing as hell, guess I've gotten used to perl for >too long :) >Thanks,
#include <stdio.h> #include <string.h> /* These are needed for the prototypes of FILE, printf, strlwr etc... */ Quote: >int >main(int argc, char *argv[]) >{ > FILE *infilep; > int num=0; > char ch[20]; > if (infilep == NULL) > { > printf("cannot open %s for input\n", argv[1]); > } > infilep = fopen(argv[1], "r");
First open the file, than check if the file could be opened. Quote: > while ((fscanf(infilep, "%s", &ch)) != EOF) > { > ch == strlwr(ch); /*converts string to lower case*/
I guess you mean: ch = strlwr(ch); You compared the pointer ch with the pointer retuned by strlwr, and did nothing with the result. TIP : compile your code before you post it, to remove errors like the above. Quote: > printf("%d %s\n", num,ch); > ++num; > } > fclose(infilep); > return(0); >}
B. v Ingen Schenau
|
Fri, 07 Dec 2001 03:00:00 GMT |
|
 |
Chris Newto #3 / 8
|
 Help with strings and parsing
[snip] Quote: >#include <stdio.h> >#include <string.h> >/* These are needed for the prototypes of FILE, >printf, strlwr etc...*/
strlwr isn't a standard function, though, so who knows which header it'll be in? Quote: >> while ((fscanf(infilep, "%s", &ch)) != EOF)
You don't need the & in &ch here. Since ch is an array-of-char, just writing ch with no & is equivalent to writing &ch[0]. Quote: >> ch == strlwr(ch); /*converts string to lower case*/ >I guess you mean: ch = strlwr(ch); >You compared the pointer ch with the pointer retuned >by strlwr, and did nothing with the result. >TIP : compile your code before you post it, to remove >errors like the above.
Friendly tip #2: Don't include non-standard library functions when posting to a standard language group :-). To the original poster: When you say you wish to remove all the "non-alpha" characters, what exactly do you mean? If you mean that you wish to remove all the alphabet characters, then it'll probably be easiest to read one character at a time instead of a line. As a starting point, after reading each character, test it with isalpha (from <ctype.h>). If it is non-alpha, add it to a buffer somewhere, otherwise discard it. When you hit a '\n', you've got to the end of the line. Then you can do your ++num and print the line in your buffer as you were before. Hope that helps, Chris -- Please reply to this newsgroup. To mail me privately, lose the "spamfree".
|
Fri, 07 Dec 2001 03:00:00 GMT |
|
 |
b.vaningensche.. #4 / 8
|
 Help with strings and parsing
On Mon, 21 Jun 1999 14:24:29 +0100, "Chris Newton" Quote:
>[snip] >>#include <stdio.h> >>#include <string.h> >>/* These are needed for the prototypes of FILE, >>printf, strlwr etc...*/ >strlwr isn't a standard function, though, so who knows which header it'll be >in?
I wasn't sure myself if strlwr was a standard function, so I looked the function up in the help-files of my compiler (BC 4.5). It said there that the function was ANSI C. It seems you cant even trust the help-files any more. It is time I got a copy of the standard. Quote: >>> while ((fscanf(infilep, "%s", &ch)) != EOF) >You don't need the & in &ch here. Since ch is an array-of-char, just >writing ch with no & is equivalent to writing &ch[0]. I missed that one. >>> ch == strlwr(ch); /*converts string to lower case*/ >>I guess you mean: ch = strlwr(ch); >>You compared the pointer ch with the pointer retuned >>by strlwr, and did nothing with the result. >>TIP : compile your code before you post it, to remove >>errors like the above. >Friendly tip #2: Don't include non-standard library functions when posting >to a standard language group :-).
As said above, I didn't know it was non-standard, because of faulty documentation. Quote: >To the original poster: When you say you wish to remove all the "non-alpha" >characters, what exactly do you mean? If you mean that you wish to remove >all the alphabet characters, then it'll probably be easiest to read one >character at a time instead of a line. >As a starting point, after reading each character, test it with isalpha >(from <ctype.h>). If it is non-alpha, add it to a buffer somewhere, >otherwise discard it. When you hit a '\n', you've got to the end of the >line. Then you can do your ++num and print the line in your buffer as you >were before. >Hope that helps, >Chris >-- >Please reply to this newsgroup. To mail me privately, lose the "spamfree".
B. v Ingen Schenau
|
Fri, 07 Dec 2001 03:00:00 GMT |
|
 |
Lawrence Kir #5 / 8
|
 Help with strings and parsing
Quote:
>[snip] >>#include <stdio.h> >>#include <string.h> >>/* These are needed for the prototypes of FILE, >>printf, strlwr etc...*/ >strlwr isn't a standard function, though, so who knows which header it'll be >in? >>> while ((fscanf(infilep, "%s", &ch)) != EOF) >You don't need the & in &ch here.
More than that it is an error to put it in. %s requires an argument of type pointer to char. &ch has type pointer to an array of chars. -- -----------------------------------------
-----------------------------------------
|
Fri, 07 Dec 2001 03:00:00 GMT |
|
 |
David Harm #6 / 8
|
 Help with strings and parsing
On Mon, 21 Jun 1999 08:33:46 GMT in comp.lang.c++, Quote:
>for the following code, how would I cut out all non alpha characters, >but leave apostrophys in the string ch?
#include <ctype.h> if (ch == '\'' || isalpha(ch)) Quote: > ch == strlwr(ch); /*converts string to lower case*/
Nope, = is assignment, == is comparison.
|
Sat, 08 Dec 2001 03:00:00 GMT |
|
 |
Shagg #7 / 8
|
 Help with strings and parsing
21 Jun 1999 12:21:46 GMT in comp.lang.c. Re: Help with strings and parsing's a cool scene! Dig it! Quote: >>int >>main(int argc, char *argv[]) >>{ >> FILE *infilep; >> int num=0; >> char ch[20]; >> if (infilep == NULL) >> { >> printf("cannot open %s for input\n", argv[1]); >> } >> infilep = fopen(argv[1], "r"); >First open the file, than check if the file could be opened.
And if the file fails to open, *quit* the program. Otherwise it will still try to read from the unopened file through a NULL pointer. This could crash the program. Also, check that there is an argv[1] before you try to use it. If the user forgets to give a filename at the command line, your program could crash here for that reason too. Quote: >> while ((fscanf(infilep, "%s", &ch)) != EOF) >> { >> ch == strlwr(ch); /*converts string to lower case*/ >I guess you mean: ch = strlwr(ch); >You compared the pointer ch with the pointer retuned by strlwr, and >did nothing with the result.
ch is not a pointer. It is an array. You cannot assign to an array. You should use strcpy() or some other method to copy the string into the array. -- ----- Dig the EVEN NEWER, MORE IMPROVED news sig!! ----- -------------- Shaggy was here! --------------- http://aardvark.apana.org.au/~phaywood/ ============= Ain't I'm a dawg!! ==============
|
Mon, 10 Dec 2001 03:00:00 GMT |
|
 |
b.vaningensche.. #8 / 8
|
 Help with strings and parsing
Thanks for the corrections. I didn't read the program careful enough, it seems. B. v Ingen Schenau On Thu, 24 Jun 1999 13:59:17 GMT,
Quote:
>21 Jun 1999 12:21:46 GMT in comp.lang.c. >Re: Help with strings and parsing's a cool scene! Dig it! >>>int >>>main(int argc, char *argv[]) >>>{ >>> FILE *infilep; >>> int num=0; >>> char ch[20]; >>> if (infilep == NULL) >>> { >>> printf("cannot open %s for input\n", argv[1]); >>> } >>> infilep = fopen(argv[1], "r"); >>First open the file, than check if the file could be opened. > And if the file fails to open, *quit* the program. Otherwise it will >still try to read from the unopened file through a NULL pointer. This >could crash the program. > Also, check that there is an argv[1] before you try to use it. If >the user forgets to give a filename at the command line, your program >could crash here for that reason too. >>> while ((fscanf(infilep, "%s", &ch)) != EOF) >>> { >>> ch == strlwr(ch); /*converts string to lower case*/ >>I guess you mean: ch = strlwr(ch); >>You compared the pointer ch with the pointer retuned by strlwr, and >>did nothing with the result. > ch is not a pointer. It is an array. You cannot assign to an array. >You should use strcpy() or some other method to copy the string into >the array. >-- >----- Dig the EVEN NEWER, MORE IMPROVED news sig!! ----- >-------------- Shaggy was here! --------------- > http://aardvark.apana.org.au/~phaywood/ >============= Ain't I'm a dawg!! ==============
|
Tue, 11 Dec 2001 03:00:00 GMT |
|
|
|