The code:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define W 5
#define N 10
/*Thanks to H.S. (stack overflow) for the remove_consecutive_characters function */
void reverse_letters_and_matrix(char matrix[W][N], int max);
void convert_tolower(char matrix[W][N]);
void remove_consecutive_characters(char* pstr);
int main()
{
int j = 0;
int i = 0;
char words[W][N];
char test[W][N];
char endword[N] = "end";
char newword[N];
char matrix1[N] = { "Hello" };
while (scanf("%9s", test), strcmp(test, endword))
{
strcpy(words[i++], test);
j++;
if (j == W)
{
break;
}
}
for (i = 0; i < j - 1; i++)
{
{
strcpy(words[i], words[i + 1]);
}
}
reverse_letters_and_matrix(words, j);
for (i = 0; i < j; i++)
{
remove_consecutive_characters(words[i]);
}
convert_tolower(words);
for (i = 0; i < j; i++)
{
printf("%s", words[i]);
printf("\n");
}
printf("End of program");
return 0;
}
void reverse_letters_and_matrix(char x[W][N], int max)
{
int i, j, n;
for (i = 0; i < max; i++)
{
for (j = 0, n = strlen(x[i]); j < n / 2; j++)
{
char c = x[i][j];
x[i][j] = x[i][n - j - 1];
x[i][n - j - 1] = c;
}
}
for (i = 0; i < max / 2; i++)
{
char temp[N];
strcpy(temp, x[i]);
strcpy(x[i], x[max - i - 1]);
strcpy(x[max - i - 1], temp);
}
return;
}
void remove_consecutive_characters(char* pstr)
{
unsigned int i;
if (pstr == NULL)
{
printf("Wrong input...\n");
return;
}
char* p = pstr;
int skip_letter = 0;
for (i = 0; pstr[i]; ++i)
{
if ((tolower(pstr[i]) == tolower(pstr[i + 1])))
{
skip_letter = 1;
continue;
}
if (skip_letter)
{
skip_letter = 0;
}
else
{
*p++ = pstr[i];
}
}
*p = '\0';
}
void convert_tolower(char matrix[W][N])
{
int i;
int j;
for (i = 0; i < W; i++)
{
for (j = 0; j < N; j++)
{
matrix[i][j] = tolower(matrix[i][j]);
}
}
return;
}
The problem: I want this program to take 5 words at most as an input.Then reverse the words,reverse the way the are printed and remove consecutive repeated characters in each word. For example,if i have as an input
This
isss
AAAa
Testtt
end
I should get
set
a
i
siht
But when I run the code I get
tset
tset
i
How can I fix that?.. I have tried in main function change the W's I had with j's but I guess that just made things worse..Also I am a newbie and do not know how to use the debugger..