Thread: replacement

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    7

    replacement

    I want to check if there is a ' ' after every '?', '!', and '.'. If there isnt i shall correct it by putting a ' '.
    In this case it removes ? ! . instead and replaces it with a ' '. Cant find the problem.
    I hope you understand.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define MAX 1000
    
    int main() {
    
    char text[MAX];
    int i, j, end;
    
    fgets(text, MAX, stdin);
    i = strlen(text); 
    end = i-1;
    
    for (j=0; j<end; j++){
      if(text[j] == '.' || text[j] == '?' || text[j] == '!'){
        if (text[j+1] != ' '){
          putchar(' ');
        }
      }
      else {
        putchar(text[j]);
      }
     }
    return 0;
    }

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    What happens in the if/else-statement when the current character is a punctuation mark? What gets printed in this case, and what gets printed in the "else" case?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    7
    Hm, well we check if the statement is true then we check adress+1(after '.'), if we dont find ' ', we print ' '.
    Else we just print out whats on the current adress and continues? I have changed
    Code:
    putchar(text[j]);
    to
    Code:
    putchar(text[j]);
    and it works better.. but i dont know why. I guess im starting to get tires here hehe. more coffee perhaps

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    7
    to
    Code:
    putchar(text[j+1]);
    even

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I guess there was some "copy'n'paste" error there - since both lines are the same.

    There's not much wrong with your code, you just have to think about what's going on in each branch of the if/else statement, and how that affects what is being output.

    I suggest you step through the code and make a clear note of exactly what is being output from some standard text, e.g.
    Code:
    Hi!test? Cool!
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    To give you another advice, what you want to do is

    1. Print the current character
    2. If the character is '!', '?' or '.' and if the next character isn't a space, print a space
    3. Repeat step 1-2 until there's no more character to treat

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by foxman View Post
    To give you another advice, what you want to do is

    1. Print the current character
    2. If the character is '!', '?' or '.' and if the next character isn't a space, print a space
    3. Repeat step 1-2 until there's no more character to treat
    Yes, I was going to get to that, but I wanted the original poster to understand HOW to understand why the code doesn't work first - part of the learning how to program is how to debug, and part of "how to debug" is to be able to understand why things happen in the code, if you see what I mean.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Delayed Replacement Sort
    By Cpro in forum C++ Programming
    Replies: 3
    Last Post: 04-12-2007, 03:36 PM
  2. string padding and replacement functions
    By George2 in forum Tech Board
    Replies: 4
    Last Post: 11-19-2006, 01:40 AM
  3. NeHe Glaux replacement??
    By Razorblade Kiss in forum Game Programming
    Replies: 4
    Last Post: 02-25-2006, 03:30 AM
  4. page replacement algorithms?
    By Extrovert in forum C++ Programming
    Replies: 1
    Last Post: 08-19-2005, 12:53 AM