Unexpected Results

This is a discussion on Unexpected Results within the C Programming forums, part of the General Programming Boards category; Hello everyone. I'm trying to write a method that will read text from a file, change any punctuation or numbers ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Location
    Scotland
    Posts
    3

    Unexpected Results

    Hello everyone.

    I'm trying to write a method that will read text from a file, change any punctuation or numbers to spaces and also preserve the existing spaces between words. I've got this far:

    Code:
    char * getText()
    {
     FILE * src = fopen("concl.txt","r");
     char * str = malloc(5001);
     char next,last=(char)"";
     int check,i=0;
     
     while((next=fgetc(src)) != EOF)
     {
      check = next;
      if(isalpha(check))
        str[i] = next;
      else {
        if(!isspace(last))
          str[i] = ' ';
      }
      i++;
      last = next;
     }
     return str;
    }
    I'm also checking that if the previous character was a space it's not going to add extra space if, for example, it finds " !". However, as soon as it encounters a double space it stops adding anything to the array and, obviously, gives an incomplete output. Can someone set me on the right track?

  2. #2
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    If I get you correctly, you want to preserve words, and replace strings containing everything else with a single space. (Including multiple spaces between two words.) This is rather easy to do.

    while(condition)
    1. If encounter(word), read whole word into string
    2. If !encounter(word), read and discard until you encounter a word. Add a space as a seperator, and continue.

    Also, you don't need check, just pass next to isalpha(). next should be declared as int to ensure EOF checking is portable. last is assigned the garbage value of a trancated string literal pointer upon startup.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  3. #3
    Registered User
    Join Date
    Nov 2006
    Location
    Scotland
    Posts
    3
    Thanks for the reply!

    What I'm trying to avoid is multiple spaces between words.

    If we were to take in:

    This is a2test to see if# this program works.

    If would output:

    This a test to see if this program works

    Numbers and punctation converted to spaces and no two spaces beside each other.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    FILE *in, *out;
    int c, last = 0;
    ... open both ...
    while( (c = fgetc( in ) != EOF )
        if( last != ' ' )
            fputc( c, fp );
        last = c;
    }
    Close them both. Delete the input file. Rename the output file.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Nov 2006
    Location
    Scotland
    Posts
    3
    Quote Originally Posted by quzah
    Code:
    FILE *in, *out;
    int c, last = 0;
    ... open both ...
    while( (c = fgetc( in ) != EOF )
        if( last != ' ' )
            fputc( c, fp );
        last = c;
    }
    Close them both. Delete the input file. Rename the output file.


    Quzah.
    ...while also removing the numbers and punctuation. I pretty much have that just now but it's still not doing what I want.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Well I'm not going to write the whole thing for you.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  2. Unexpected results from function
    By ajdspud in forum C++ Programming
    Replies: 2
    Last Post: 11-27-2005, 03:19 PM
  3. Unexpected results
    By Syked4 in forum C Programming
    Replies: 2
    Last Post: 06-07-2005, 09:41 PM
  4. Unexpected results using argc / *argv[]
    By Morgan in forum C Programming
    Replies: 1
    Last Post: 09-11-2003, 01:38 AM
  5. Greenhand want help!
    By leereg in forum C Programming
    Replies: 6
    Last Post: 01-29-2002, 05:04 AM

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