Did you know LQ has a Linux Hardware Compatibility List?
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


LinkBack Search this Thread
Old 08-31-2004, 06:03 AM   #1
Registered: May 2004
Posts: 80

Rep: Reputation: 15
word count

how to find and print only the first word from each line of a file using C
Old 08-31-2004, 07:14 AM   #2
Senior Member
Registered: Oct 2003
Location: Zurich, Switzerland
Distribution: Debian/unstable
Posts: 1,357

Rep: Reputation: 47
Good Lord, C is totally wrong choice for that.

Solution in 6 other languages:
awk '{print $1}' foo.txt
sed 's/^\([^ ]*\).*$/\1/' foo.txt
perl -pe '($_) = split(" ", $_);  $_=~s/$/\n/' foo.txt
or other perl implementation:
perl -pe '$_=~ s/^(\w+).*$/$1/;' foo.txt
tcl: (inside tclsh)
while {[ gets stdin line] >= 0} { puts [lindex [split $line " "] 0] }
shell script/coreutils:
cut -f1 -d' ' foo.txt
python -c $'import sys;\ndef fword(x): print x.split(" ")[0].strip();\nmap(fword,sys.stdin.readlines());' < foo.txt
Do you still want to do that in C?
Old 08-31-2004, 07:23 AM   #3
Registered: Jul 2004
Location: Pakistan
Distribution: Ubuntu
Posts: 185

Rep: Reputation: 30
Open a file
then get a line in buffer using getline()------------------> see " man getline " for details.
then use strchar() to locate a space character --------------> see " man strchar()"
This will give you the index of first space charecter in that buffer.
strncpy ( first_word , buffer , index_of_space);
printf (first_word)
And then read the next line until the end of file. The code will be something like.

while( feof ( fileptr ) )
getline ( buffer, length , fileptr);
strncpy ( first_word , buffer , strchar( *buffer , " " ) );
printf (first_word);

There may be more efficient way to do that.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
word count in a line pantera Programming 4 08-25-2004 01:14 PM
Should posts in general count on your post count? Joey.Dale General 16 01-27-2004 01:31 AM
Really basic OOo or Staroffice Q re: word count Calum Linux - Software 8 06-04-2003 05:57 AM
Word count in paragraph - Open Office, Sutekh Linux - Software 10 04-19-2003 10:27 PM
Viewers for Word, Word Perfect, etc. Claude Williams Linux - General 2 11-22-2001 04:01 AM

All times are GMT -5. The time now is 07:57 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration