Thread: Need Help with C Programming!

  1. #1
    Registered User
    Join Date
    Nov 2016
    Posts
    11

    Need Help with C Programming!

    I am currently writing an assignment that takes a certain text and scrambles the middle letters of every word, but leaves the first and last character of the word alone. The code I have come up with so far works well for some words, but not others, particularly 4 letter words or words with punctuation. Any help on what to do to fix my code would be greatly appreciated! Help.txt

    The text the code is supposed to scramble: A6P1_2016_TestingSherlock.txt

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <stdbool.h>
    #include <ctype.h>
    #include <string.h>
    #include <time.h>
    
    #define INPUT_FILENAME    ("A6P1_2016_TestingSherlock.txt")
    #define OUTPUT_FILENAME   ("A6P1_2016_SherlockEncoded.txt")
    
    void process_file(FILE* ifp, FILE* ofp) {
    	printf("Begin file processing\n");
    	char text[10000];
        while((fscanf(ifp, "%s", text)!= -1)){
        	int i;
        	char c;
        	int length = strlen(text);
        	c = text[1];
        	if(length>3){
        		for (i=1; i<10000; i++){
        			if(i==length-2){
        				text[i]= c ;
        			}
        			else if(i==length-1){}
       				else{
       					text[i]=text[i+1];
       				}
       			}
        	}
        	printf("%s ", text);
            fprintf(ofp,"%s ",text);
        }
        printf("\n");
    	printf("End file processing\n");
    } /* process_file */
    
    int main(void) {
    	printf("Starting text encoding...\n\n");
    	unsigned int seed = (unsigned int)time(NULL);
    	srand(seed);
    
    	FILE* ifp = fopen(INPUT_FILENAME, "r");
    	if (ifp == NULL) {
    		printf("Cannot open input file %s\n", INPUT_FILENAME);
    		exit(EXIT_FAILURE);
    	} /* if */
    
    	FILE* ofp = fopen(OUTPUT_FILENAME, "w");
    	if (ofp == NULL) {
    		printf("Cannot create output file %s\n", OUTPUT_FILENAME);
    		exit(EXIT_FAILURE);
    	} /* if */
    
    	process_file(ifp, ofp);
    
    	fclose(ofp);
    	fclose(ifp);
    	printf("\nEncoding finished.\n");
    	return EXIT_SUCCESS;
    } /*main*/
    Last edited by Salem; 11-17-2016 at 02:01 AM. Reason: inlined code

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,663
    Some thoughts.

    1. Your while loop should test for != EOF, not != -1

    2. If you want to take care of punctuation, then consider making use of the ispunct() function in ctype.h.

    3. your for loop should be < length (or something similar to length), not 10000.

    4. you need to add a \0 to the string after you've done all your processing.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Nov 2016
    Posts
    11
    Thanks for the tip! It turned out to be the for loop < length issue you mentioned that was causing the problem!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 09-11-2012, 01:03 AM
  2. Replies: 4
    Last Post: 12-11-2011, 04:25 PM
  3. small programming job VCPP / Object Oriented Programming
    By calgonite in forum Projects and Job Recruitment
    Replies: 10
    Last Post: 01-04-2006, 11:48 PM
  4. Total newb to programming here... Question about the many programming languages. Ty!
    By tsubotakid1 in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 10-05-2003, 10:32 AM

Tags for this Thread