Thread: Can anyone answer please

  1. #1
    Registered User
    Join Date
    May 2015
    Posts
    1

    Question Can anyone answer please

    Using C language, please write a code snippet that processes a String, and replaces every 5th character


    with “X”.


    If the input is “The Cat Sat on the Mat”, the output should be


    “The Xat SXt onXthe Xat”

  2. #2
    Registered User
    Join Date
    Sep 2011
    Location
    Athens , Greece
    Posts
    357
    First of all you need to read the string from the user. So you want a function for it. I suggest you using fgets function.

    fgets - C++ Reference

    After all you need to make your process in your string. So you can declare a variable with the appropriate position (here the position is 4)

    Code:
      #define POS 4
     int position = POS;
    every POS-ed times you will put the 'X' character in your string. Make a loop that increase its iteration value every POS-ed times. In the body of this loop you can replace with the 'X' character.

    Code:
      array_that_hold_your_string[i] ='X';  // i +=POS
    Note that I am using a directive in order to make the POS. Doing this is a good programming practice if you want to change the position in the future for example if you want to replace the 8th character and then the 16th and so forth in your string. The only thing you need to do is to change the number 8 for example and this number will be changed to all points that use the POS in your program.
    Last edited by Mr.Lnx; 05-03-2015 at 05:33 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 08-13-2012, 03:19 AM
  2. Help with comparing answer to math problem and user's answer
    By Jake Garbelotti in forum C Programming
    Replies: 6
    Last Post: 07-20-2012, 10:12 PM
  3. Where Can I Get The Answer?
    By MartinLiao in forum C++ Programming
    Replies: 3
    Last Post: 07-23-2004, 01:19 PM
  4. Please answer this
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 02-25-2004, 11:43 AM
  5. Trying to answer
    By gustavosserra in forum C++ Programming
    Replies: 3
    Last Post: 04-19-2003, 10:33 PM