Thread: Complete C noob...and completely stuck lol

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    7

    Question Complete C noob...and completely stuck lol

    OK, time to have a laugh. I'm a first year digital forensics student and part of my course deals with C (ANSI C) programming. I have been given an exercise whereby I have to write a C program that will open a file, read it and either find and delete or find and replace characters (I.e- wherever I find a lowercase at the start of a sentence it should be made uppercase ( ^ 0x20 maybe??), and any instance where 'i' is on it's own it should also be made uppercase. The text file contains lots of characters such as & etc in the middle of words and needs punctuating('.' periods) etc. I thought about using an array...but I don't know how to use them that well. What I got so far is -:
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <sys\stat.h>
    
    	void main (void)
     {
    	FILE *inputFile; 				
    	FILE *outputFile;
    	char input1,input2,input3;
    
    		
    
            	inputFile=fopen("inputfile.txt","r");
            	outputFile=fopen("outputfile.txt","w");
            	input1=fgetc(inputFile);	
            	input2=fgetc(inputFile);
            	input3=fgetc(inputFile);
            	fprintf(outputFile,"%c",input1,input2,input3);
    			
    	do
    	if (input1==' ' && input2=='i' && input3==' ')
    		{	
    			input1 = input2 ^ 0x20;
    		}			
    		while (input1,input2,input3 !=EOF);
    
    }
    This only reads the first character of the file and won't read past it or correct anything...any help would be greatly appreciated.
    (I have attached the file with the text in)

    Thanks to anyone who can help

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    1. int main(void) - read FAQ
    2. fgetc returns int - make variable you store the input the same type
    3. put your input inside the loop, for example
    Code:
    while((input = fgetc(inputFile)) != EOF)
    {
       /* process the char */
    }
    4. make your printf format match the variable list - for now you have 1 format specifier and 3 variables
    5. use toupper fuction to convert to upper case
    6. you should implement some state-machine to decide when the conversion should be performed
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I don't see why you would need to read three character at a time in the first place.

    Just get a character, decide if it should be ignored or converted to upper-case and write it to the output file if needed.

    The hard thing is determining whether you are at the start of a sentence. I would use a variable for that, set it to TRUE if I encounter a full stop and uppercase the next non-whitespace character and set the flag to FALSE.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    Registered User
    Join Date
    Mar 2008
    Posts
    7
    I thought fgetc would return a char?? oh well, I did have int main (void) initially, but someone told me to change it. so the while func mentioned there would appear after a do loop presumably?, would that be the same as the do loop already there? I haven't come across toupper either so I'm not sure how to use that. Like I said I'm a complete noob and our lecturer isn't very good at explaining things either. Sorry if that just adds to confusion lol

  5. #5
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    One thing that will help you very much is online references.
    http://www.cplusplus.com/reference/clibrary/
    The things you need to know for using them are a little bit about the basic C - types.
    Every time you think you need something but don't know how.
    I haven't come across toupper either so I'm not sure how to use that.
    A small search will provide information. Remember that every function works like a black box.
    You put something in, and you take something out. The most common starting mistakes is because people don't look at the definition of the function to see what it gets and what it returns.
    If you know those little bits of information, very very complex problems might be solved even with one statement.
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

  6. #6
    Registered User
    Join Date
    Mar 2008
    Posts
    1
    How would you get the program to recognise the first letter in a sentence etc?

  7. #7
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    The question imposes another question. How do you define a character sequence as a sentence?
    Does ';' count as a terminator, or only '.' and '?'.
    When that is resolved then i would define the first letter of a sentence as:
    Either the first non-whitespace letter of a string, or the first non-whitespace letter of a string that follows a sentence-terminator symbol.
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. .ico (COMPLETE NOOB)
    By Rage7531 in forum Windows Programming
    Replies: 2
    Last Post: 05-13-2002, 05:07 PM