Click here to Skip to main content
15,558,870 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The main question was to write a code which replaces the spaces of certain texts with the character "-", but I've done that. The only thing left, is that after the it changes the spaces for "-", it should count how many there are.
So, if there are 5 -'s, have an out put that says "You have replaced 5 spaces"
not counting spaces, but counting -'s. I just want this code added to my existing code. I've tried doing that but it's really hard adding code without messing it all up so it wouldn't compile.

What I have tried:

Here's my attempt at this:
C++
#include<stdio.h>
#include<ctype.h>
#include<conio.h>
int main()
{
	int new_char;
	char t;
	int ctr = 0;
	char str[100];
	printf("\n Replace the spaces of a string with a specific character :\n");

	printf(" Input a string:");
	fgets(str, sizeof str, stdin);
	printf(" Input replace character : ");
	scanf_s("%c", &t);
	printf(" After replacing the space with  %c the new string is :\n", t);
	while (str[ctr])
	{
		new_char = str[ctr];
		if (isspace(new_char))
			new_char = t;
		putchar(new_char);
		ctr++;		
	}
	printf("\n\n");
	return 0;
}
Posted
Updated 9-Nov-20 8:36am
v2
Comments
jeron1 9-Nov-20 13:42pm    
How about having a new integer variable called 'replaced', initialized to zero at startup and incremented anytime you execute the following line,
 new_char = t;
Joe Woodbury 9-Nov-20 15:23pm    
isspace includes CR and LF. Is this intended?

Also note that scanf_s should have an added parameter: scanf_s("%c", &t, sizeof(t));

1 solution

that is really simply:
C++
//outside the loop
int count = 0;

if (isspace(new_char)) {
	count++
	new_char = t;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900