C program to read a string and replaces every 5th character with “X”

The below program in C will accept a string from user. It will then process the string and replace every 5th character of the string with a character “X”.

Here is the C code.

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
	char *str;
	int i;
	clrscr();
	printf("Enter a string: ");
	gets(str);
	for(i=0;i<strlen(str);i++)
	{
		if((i+1)%5 == 0)
		{
			str[i] = 'X';
		}
	}
	puts(str);
	getch();
}

C Output

Enter a string: This is a C Program
ThisXis aXC PrXgram

C Programming is brought you by LearnCOnline.com

You may also like...

4 Responses

  1. NURSHAFIKA BINTI MOHD YATIM says:

    calculate the total price for items :
    – price RM2 and RM5
    – price >RM5

    So , how to solve this problem using file operation and control statements ?

  2. Sateesha says:

    Function shall sum members of given one-dimensional array. However, it should sum only members whose number of ones in the binary representation is higher than defined threshold (e.g. if the threshold is 4, number 255 will be counted and 15 will not)
    – The array length is arbitrary
    – output the results to the stdout

  1. May 4, 2015

    […] Read a string and replaces every 5th character with

Leave a Reply

Your email address will not be published. Required fields are marked *

FREE C Cheatsheet - Speed Up Your C Programming.

FREE C Cheatsheet - Speed Up Your C Programming.

Download a 7-page free cheat sheet for easy and quick access to C Concepts, Snippets, and Syntax.

Thank you! Check you inbox and access your cheat-sheet