Hi, i'm really new to (C) programming.
I'm practicing by writing a code that will take a string, and then send the individual words of that string, to a function to be printed. The problem is that because i override the characters in the respective elements of the array, if one of the words is shorter than the one before it, it carries across the excess letter.
E.g "what you want" gives me :
what you want
what
yout
want
I'm thinking i need to re assign the value 0 to my whole array (since apparently i cant re-initialize in C), problem is the loop I'm using doesn't work. When i compile it in Visual C++, i get the following message.
"Run-Time Check Failure #2 - Stack around the variable 'textblock' was corrupted."
You'll see the loop commented out in the second If statement in my While.
Warning. What your about to see is a very ugly, and extremely inefficient chunk of code. Please be gentle.
Code:#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int main(int argc, char *argv[]){
char mytext[21] = {"what you want"};
char textblock[21] = {0};
int wordplace(char textblock[]);
printf("%s\n", mytext);
int i = 0;
int j = 0;
int f = 0;
while((i<22) && (j<22) && (mytext[i] != '\0')){
if(( mytext[i]>='a' && mytext[i]<='z' ) || ( mytext[i]>='A' && mytext[i]<='Z' )){
textblock[j] = mytext[i];
}
i++;
j++;
if (mytext[i] == ' ' || mytext == 0 || mytext[i] == '\0'){
mytext[i] = '\0';
wordplace(textblock);
i++;
j = 0;
//while (j<22){
//textblock[j] = 0;
//j++;
}
}
}
return 0;
}
int wordplace(char textblock[]){
printf("%s\n", textblock);
return 0;
}
Please help. Thank You.