C HELP!

Alex

Diamond Member
Oct 26, 1999
6,995
0
0
is there any standard c function for trimming the end of strings?
i use fgets() to read a line from a stream and since i dont know how long the stream is i malloc enough space for a 200 character line (worse-case scenario by far)
afterwards, id like to trim all the blank spaces after the end of the string so as not to waste memory.
i know it hardly makes a practical impact but id like to be as efficient as possible.



also

is there any standard algorithm for doing regular expression searches on text files? i have to implement this in C and it will be very difficult without one...




thanks! :)
 

AgentEL

Golden Member
Jun 25, 2001
1,327
0
0
int trim(char s[])
{
int n;

for (n = strlen(s)-1; n>=0; n--)
if (s[n] != ' ' && s[n] != '\t' && s[n] != '\n')
break;

s[n+1] = '\0';
return n;
}

If you have K&R, it's on page 65 ;-)
 

Alex

Diamond Member
Oct 26, 1999
6,995
0
0
whoa that is amazing, i read through the code and tried to understand it and im not quite sure how it works but i copied and pasted and changed the return type to char * and it works like a charm! sweet! thanks!

oh by the way can you elaborate on K&R? since this is for an assignment im thinking of properly citing where i got the algorithm since its not my code... thanks! :)

Originally posted by: AgentEL
int trim(char s[])
{
int n;

for (n = strlen(s)-1; n>=0; n--)
if (s[n] != ' ' && s[n] != '\t' && s[n] != '\n')
break;

s[n+1] = '\0';
return n;
}

If you have K&R, it's on page 65 ;-)

 

oog

Golden Member
Feb 14, 2002
1,721
0
0
K&R is "kernighan and ritchie" which is shorthand for "the C programming language" by brian kernighan and dennis ritchie. it's a good book, but a little skimpy.

you may also want to check that you don't try to access memory beyond the number of characters you know are in your string. otherwise, you may be accessing invalid memory.
 

AgentEL

Golden Member
Jun 25, 2001
1,327
0
0
It's The C Programming Language, 2nd edition, by Kernighan and Ritchie (K&R). It's a pretty standard book about C. Pretty good resource especially if you're going to be using C a lot.
 

Replicon

Member
Apr 23, 2002
152
0
71
As for your second question, if you're using unix/linux, have a look at the regex library (man regex). You give it a regular expression, and compile it into a format it likes... and then you can match stuff against it pretty easily. Here's a little tutorial:

http://metahtml.sourceforge.net/documentation/regex/

I am sure there's some windows variant somewhere, I just never looked.
 

BFG10K

Lifer
Aug 14, 2000
22,709
3,003
126
Why would it return a char *? You pass in a char *.
Not only that but N is the string's new length which could be useful if you plan on doing concatenation.
 

agnitrate

Diamond Member
Jul 2, 2001
3,761
1
0
I don't see how that formula saves memory. It just concatenates a string.

The gets() function reads characters from the standard input
stream (see intro(3)), stdin, into the array pointed to by
s, until a newline character is read or an end-of-file con-
dition is encountered. The newline character is discarded
and the string is terminated with a null character.

fgets() does this :

memory 0x001 : [ c + + ' ' r o c k s '\0' null null null null null null null null null null] <-- 20 spaces allocated

The function you posted doesn't do anything. It starts at the end of the file and counts down to 's' in our case, and then puts a \0 where there already should be one. What you should do is instantiate the char* array full of '\0' initially and read into it. Then get its length, copy it over to another char* array of the designated size and delete the old array for memory purposes. This one does not save memory at all.

-silver