Basically, what I'm trying to do is take a line of text from the user as input, then spew it back, but with "I am" replaced with "You are". There are a few printf's in here that are just there to show me what my own program is doing, for debugging purposes, but I can't seem to get this code working with more than one "I am" in the input, even though it should. I know there are some things I could be doing more efficiently, so if you see them, please point them out.
Please have a look and tell me what you think I should change. Thanks in advance
Code:
#include <stdio.h>
#include <string.h>
char recent[99];
int main(void) {
int i, q[3], w = 0, x, b[3];
/* Set arrays to zero */
for (i = 0; i <= 99; ++i) {
recent[i] = -1;
}
for (i = 0; i <= 3; ++i) {
q[i] = -1;
b[i] = 0;
}
/* Get a string from the user, then check for 'I' */
fgets(recent, 100, stdin);
recent[(strlen(recent) - 1)] == '\0';
for (i = 0; i <= 99; ++i) {
if (recent[i] == 'I') {
q[w] = i;
++w;
}
}
/* Check to see if 'I' is followed by ' am' */
for (i = 0; i <= 3; ++i) {
if (recent[(q[i] + 1)] == ' ') {
if (recent[(q[i] + 2)] == 'a') {
if (recent[(q[i] + 3)] == 'm') {
printf("\nq[%i] is an \"I am\" statement.", i);
b[i] = 1;
}
}
}
}
/* Replace 'I am' with 'You are' */
for (i = 0; i <= 3; ++i) {
if (b[i] == 1) {
for (x = 99; x >= q[i]; --x) {
recent[(x + 3)] = recent[x];
}
recent[(q[i])] = 'Y';
recent[(q[i] + 1)] = 'o';
recent[(q[i] + 2)] = 'u';
recent[(q[i] + 3)] = ' ';
recent[(q[i] + 4)] = 'a';
recent[(q[i] + 5)] = 'r';
recent[(q[i] + 6)] = 'e';
}
}
printf("\n\nq[0] = %i\nq[1] = %i\nq[2] = %i\nq[3] = %i\n", q[0], q[1], q[2], q[3]);
printf("\n\nb[0] = %i\nb[1] = %i\nb[2] = %i\nb[3] = %i\n", b[0], b[1], b[2], b[3]);
/* Print final text */
printf("\n%s\n", recent);
/* System pauses */
printf("\n");
system("PAUSE");
return 0;
}