Function for surrounding words with '`'

This is a discussion on Function for surrounding words with '`' within the C Programming forums, part of the General Programming Boards category; I want to build a function that reads text from a text file, surrounds all the words with '`', puts ...

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    5

    Function for surrounding words with '`'

    I want to build a function that reads text from a text file, surrounds all the words with '`', puts every word on a single line and saves the file to another one. I'm a beginner and I ended up with this code. It just opens the file, and puts the words on single lines...

    Code:
            void replace() {            
                char ch;
                FILE *fp,*fp2;
                fp = fopen("C:\\test.txt", "r");
                   if (!fp) {
                  printf("\nFile test.txt cannot be opened.\n");
                 }
                 fp2 = fopen("C:\\test2.txt", "w");
                   if (!fp2) {
                  printf("\nFile test2.txt cannot be opened.\n");
                 }
    
    
                 while((ch=getc(fp))!=EOF) {
                  if(isspace(ch)) {
                  ch='\n';
                  }
                  fputc(ch,fp2);
                  }
                 fclose(fp);
                 fclose(fp2);
                 printf("''test2.txt'' was created.\nPress Enter to go back to the Menu...");
                 getchar();
                 system("cls"); //*I know I shouldn't use system("cls"), but let's not comment it now.. :) *//
                 main(); //*returns to main(), which has a menu *//
                 exit(0);
    
    
                }
    How can I do that word formatting? Any ideas?

    P.S. An example:
    If the text file has:
    Code:
    The Quick Brown Fox
    I want the new file to be:
    Code:
    `The`
    `Quick`
    `Brown`
    `Fox`

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,762
    If you understand what this part does:
    Code:
         while((ch=getc(fp))!=EOF) {
          if(isspace(ch)) {
          ch='\n';
          }
          fputc(ch,fp2);
          }
    you'll figure out where to write the quotes.

  3. #3
    Registered User
    Join Date
    Dec 2011
    Posts
    5
    Code:
         while((ch=getc(fp))!=EOF) { //* - reads characters from the file until end of file
          if(isspace(ch)) { //* - if (ch) is a whitespace
          ch='\n'; //* - replace that whitespace with a new line character
          }
          fputc(ch,fp2); //* - writes the data into the other file 
          }
    I think I understand what it does, because I did it myself, but I still can't figure out how to do that word surrounding.. That's why I asked here.. I'm maybe too stupid for C..

  4. #4
    Registered User
    Join Date
    Dec 2011
    Posts
    5
    Sorry for bumping my thread, but I really have to get this program working..

  5. #5
    Registered User
    Join Date
    Sep 2011
    Posts
    111
    If you want the following

    "This is a string" to end up like

    This
    is
    a
    string

    Then take a look at the strtok() function. You can setup your print statement as '%s'.

  6. #6
    Registered User
    Join Date
    Dec 2011
    Posts
    5
    Thank you very much! You have one ice-cold beer from me..

  7. #7
    Registered User
    Join Date
    Dec 2011
    Posts
    5
    But first I have to read those characters from the file into a string.. What shoud I use - fgets()?
    Last edited by plamenbv; 12-03-2011 at 03:20 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 34
    Last Post: 06-22-2011, 08:07 AM
  2. Replies: 7
    Last Post: 10-01-2010, 05:09 PM
  3. Reading words and analyzing words from file
    By desmond5 in forum C Programming
    Replies: 7
    Last Post: 02-26-2008, 03:51 PM
  4. 2 questions surrounding an I/O file
    By Guti14 in forum C Programming
    Replies: 2
    Last Post: 08-31-2004, 12:21 AM
  5. extracting words from an array of words
    By axon in forum C++ Programming
    Replies: 2
    Last Post: 04-02-2003, 11:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21