But the one before didn't have any limits on input or output size.
Since we're all having fun though, here's another one, even though I prefer the first version.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
struct repl_detail {
int dots;
int exclaims;
};
void *xmalloc(size_t bytes) {
void *p = malloc(bytes);
if (!p) {
perror("xmalloc");
exit(EXIT_FAILURE);
}
return p;
}
struct repl_detail repl(const char *input, char **output)
{
struct repl_detail count = { 0, 0 };
char *out = *output;
while (*input) {
switch (*input) {
case '.':
count.dots++;
if (out) {
*out++ = '!';
}
break;
case '!':
count.exclaims++;
if (out) {
*out++ = '!';
*out++ = '!';
}
break;
default:
if (out) {
*out++ = *input;
}
break;
}
input++;
}
if (out) {
*out = '\0';
}
return count;
}
int main(void) {
char input[2048] = ""; // whatever you prefer.
fgets(input, sizeof input, stdin);
// Sort of like snprintf(), if the output is NULL, then it will count the number of replacements it would make only.
char *output = NULL;
struct repl_detail deets;
deets = repl(input, &output);
output = xmalloc(strlen(input) + (deets.exclaims * 2) + 1);
deets = repl(input, &output);
printf("%s\n%d .'s and %d !'s.\n", output, deets.dots, deets.exclaims);
free(output);
return EXIT_SUCCESS;
}
I used it on one of the OP's posts
C:\Users\jk\Desktop>a.exe < foo.txt
Ahhh!!!!!! Thank you, @algorism! I don't know why i ended up putting the putchar() in the beginning! I put it with the else in the if else program so why not here I have know idea! The if(ch == '\n') continue waz there because i thought i needed it to chomp the newline character in the input buffer after hitting enter! i had that problem a few programz ago and a programmer gave me that fix to clear the buffer! I read on here that it's a common problem with us noobs and getchar() is another fix! I see I don't need it here! The commentz at the end bracketz were for me cuz i was losing my place and just forgot to delete them! The argc and argv were there because that's what Xcode had set up! The newline at the beginning of the printf() is for the very same reason you are annoyed with the cursor placement!!! it was printing on the same line as the putchar(ch) output! I will add one to the end! Now, as to my English as a 1st or 2nd language!!! maybe i was having fun with 's' soundz!!! maybe I'm a nonconformist to the English language and like to dot my t'z and cross my eyez!!! maybe I'm hooked on ebonicz!!! or iz it phonicz? Maybe i chose to be creative and use the right side of my brain just for poopz and gigglez! All joking aside @algorism!!! thank you for helping me see my stoopid mistakez and brain fartz!! Greatly appreciated!!
30 .'s and 5 !'s.