LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices


Reply
  Search this Thread
Old 08-24-2004, 09:37 PM   #1
pantera
Member
 
Registered: May 2004
Posts: 80

Rep: Reputation: 15
word count in a line


how to find and print only the first word from each line of a file using C programming

Last edited by pantera; 08-24-2004 at 09:56 PM.
 
Old 08-24-2004, 09:50 PM   #2
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
If you separate words by using only spaces (not tabs, not punctuation, and not any other characters), you could do this:

sed "s/^[ ]*\([^ ]\+\).*/\1/" some_file
 
Old 08-25-2004, 05:26 AM   #3
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,358

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
This may be a homework qn (cf: http://www.catb.org/~esr/faqs/smart-....html#homework) ...
However, as hinted by Dark_Helmet, the 1st step is to determine the separator char(s) between words and go from there....
There are several ways of doing this, but personally I'd prob consider the strtok() fn.... depending on the answer to the 1st step qn.
 
Old 08-25-2004, 10:49 AM   #4
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 36
Code:
#include <stdio.h>
/* no error checking at all; assumes no leading spaces */          
/* since it's homework - you add error checking */    
/* file =mycode.c */

int main(int argc, char *argv[]){
    char tmp[512]={0x0};
    FILE *in=fopen(argv[1],"r");
    while(fgets(tmp,sizeof(tmp), in)!=NULL){
        for(int i=0;tmp[i]!=' ';i++) printf("%c",tmp[i]);
        printf("\n");
    }
    fclose(in);
    return 0;
}
usage: mycode /path/to/file
 
Old 08-25-2004, 01:14 PM   #5
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
Hehehe... edited after my response to clarify writing it in C.

It's left as an exercise for the reader to execute shell commands within their program (man popen is a good start).

Who knows, it might get a few "creativity points", then again...
 
  


Reply



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



Similar Threads
Thread Thread Starter Forum Replies Last Post
word count pantera Programming 2 08-31-2004 07:23 AM
How to count line numbers recursively in a directory? puzz_1 Linux - General 1 07-01-2004 09:43 AM
Count number of line in a file. philipina Programming 7 03-18-2004 05:04 PM
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

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:10 AM.

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