I have to take a phrase from the user, and print it out in an inverted triangle with spaces in between. My program takes the phrase, stores it into an inputBuffer (array of char), and then creates a new array double the size of string. It fills up the first half with spaces, and the second half with the string itself. I want to print out the strLength char from the new array strLength times, just by shifting the range of strLength to (strLength*2-1) by 1 each time to the left. This ensures that in the first iteation, only the whole string is printed, second time one space at the beginning and one char at the end is not printed, as so on.
Currently I am getting an error that even through strLength is an int variable, when I use it to create the new array it's apparently NOT a constant value.
int main(void) {
char inputBuffer[256];
char *pointer = inputBuffer;
char *temp = pointer;
int strLength = 0;
printf("enter your word: ");
scanf("%s", pointer);
//Calculate string length
while (*temp++) strLength++;
//Create an array double the size, first half for white spaces, and second half for the phrase.
char inputString[strLength * 2];
// ERROR: above expression inside the index must be a constant value.
int i, j;
//First half of array is number of spaces == number of char in phrase
for (i = 0; i < strLength; i++) {
inputString[i] = ' ';
}
//Reinitialize temp to use instead of pointer & put the string in the second half of inputString[]
temp = pointer;
for (j = 0; j < strLength; j++) {
inputString[i++] = *temp++;
}
//Just print the strLength indexes of inputStrng[] starting from half to end, and keep shifting the range by 1 position to the left.
for (i = strLength; i < (strLength * 2); i--) {
for (j = 0; j < strLength; j++) {
putchar(inputString[i + j]);
putchar(' ');
}
putchar('\n');
}
return 0;
}