Can anyone tell me why the output here is garbled?
It's supposed to place words in one container, everything else in the next, so on and so on till the array of structures is filled. Instead, well, compile for yourself and see!
Code:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
typedef struct temporary
{
char t[25];
};
int main()
{
char buffer[]=" This is 1very long sentence. It may be boring 4 you to read, and you may care < about what it says but it is #1 for Testing!!!";
int start_word, u = 0;
int count=0;
///////////// First Count ///////////////////////////
if( isalpha(buffer[0]) ){start_word = 0;} // <---Precondition
else if( !isalpha(buffer[0]) ){start_word = 1;} // <---
for( yy = 0; yy< strlen(buffer);yy++)
{
if( start_word == 0)
{
if( !isalpha(buffer[yy]) )
{
start_word = 1;
count++;
}
}
else if( start_word == 1)
{
if( isalpha(buffer[yy]) && buffer[yy] != '\n' )
{
start_word = 0;
count++;
}
}
}
/////////////// End Counting ///////////////////////
char tem[25];
struct temporary temp[count]; <---use count
int s = 0;
if( isalpha(buffer[0]) ){start_word = 0;} // <---Precondition
else if( !isalpha(buffer[0]) ){start_word = 1;} // <---
for( yy = 0; yy< strlen(buffer);yy++)
{
if( start_word == 0)
{
if( isalpha(buffer[yy]) )
{
tem[u] = buffer[yy]; u++;
}
if( !isalpha(buffer[yy]) )
{
///tem[u-1] = '\0';
strcpy(temp[s].t,tem);
start_word = 1;
u = 0;
s++;
strcpy(tem,"");
}
}
else if( start_word == 1)
{
if( !isalpha(buffer[yy]) )
{
tem[u] = buffer[yy]; u++;
}
if( isalpha(buffer[yy]) )
{
///tem[u-1] = '\0'; <--didn't help
strcpy(temp[s].t,tem);
start_word = 0;
u = 0;
s++;
strcpy(tem,"");
}
}
}
for(i=0;i<count;i++){ printf("%s",temp[yy].t); getch(); }
return 0;
}