D
djinni
It's been a while since I've written anything in C, and
I was hoping to get some feedback on the following code.
As far as I can tell, the program works fine, but I'm not
sure I'm cleaning all the memory up properly, and I think
the "tokenize_my_string" function can probably be made better.
This program prompts the user for a text sentence,
then re-orders the words in a random fashion...
======================================================
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#define DEBUG 0
#define MAX_NOF_TOK 256 /* max number of words */
#define MAX_TOK_LEN 256 /* max word length */
#define MAX_MSG_LEN (MAX_NOF_TOK * MAX_TOK_LEN)
#define RAND_FACTOR 5
void sort_my_string( char** sentence, int word_count );
int tokenize_my_string( char* buffer,
char** sentence,
int* word_count );
int main(void)
{
char buffer[ MAX_MSG_LEN ];
char *sentence[ MAX_NOF_TOK ];
char *first_string;
int i=0, word_count=0;
/*
** output instructions
*/
fprintf( stdout, "Type in the text:\n\t" );
fflush( stdout );
/*
** get input string from user
*/
if( fgets( buffer, MAX_MSG_LEN, stdin ) == NULL )
{
fprintf(stderr, "learn how to type jackass!\n" );
return EXIT_FAILURE;
}
/*
** store buffer so strtok doesn't walk all over it
*/
first_string = malloc(strlen(buffer)+1);
if( first_string == NULL )
{
fprintf(stderr, "you need more memory dude\n");
return EXIT_FAILURE;
}
memcpy( first_string, buffer, strlen(buffer)+1 );
/*
** pesky newline...
*/
if( buffer[strlen(buffer)-1] == '\n' )
{
buffer[strlen(buffer)-1] = '\0';
}
/*
** break up the string into words
** and store each word as an element
** in the "sentence" array
**
** remember that this allocates memory for "sentence",
** so don't forget to free() it!!!
*/
if( tokenize_my_string( buffer, sentence, &word_count ) )
{
fprintf(stderr, "tokenize_my_string failed!\n" );
free( first_string );
first_string = NULL;
return EXIT_FAILURE;
}
sort_my_string( sentence, word_count );
fprintf(stdout,"randomized string looks like:\n\t");
/*
** print the sorted string
*/
for( i=0; i<word_count; i++ )
{
fprintf(stdout, "%s ", sentence );
}
fprintf(stdout, "\n");
/*
** clean up memory
*/
for( i=0; i<word_count; i++ )
{
free( sentence );
sentence = NULL;
}
free( first_string );
first_string = NULL;
return EXIT_SUCCESS;
}
void sort_my_string( char** s, int word_count )
{
char *tmp;
int ind_from, ind_to;
int rand_ind = (word_count * RAND_FACTOR);
srand( time(0) );
while( --rand_ind > 0 )
{
ind_from = rand() % word_count;
ind_to = rand() % word_count;
if( ind_from == ind_to )
continue;
tmp = s[ind_to];
s[ind_to] = s[ind_from];
s[ind_from] = tmp;
}
return;
}
int tokenize_my_string( char* buffer,
char** sentence,
int *word_count )
{
char *str_p;
int i;
/*
** get the first token...
*/
if( (str_p = strtok( buffer, " " )) == NULL )
{
fprintf( stderr, "it broke here: %d\n", __LINE__ );
return 1;
}
(*word_count) = 1;
/*
** get space for the first token in our sorted string
*/
sentence[0] = malloc( strlen(str_p)+1 );
if( sentence[0] == NULL )
{
fprintf(stderr, "malloc failed: %d\n", __LINE__ );
return 1;
}
memcpy( sentence[0], str_p, strlen(str_p)+1 );
/*
** get the rest of the tokens
*/
while( ((str_p = strtok(NULL, " "))!=NULL) &&
(++(*word_count) < MAX_NOF_TOK) )
{
/*
** make some room
*/
sentence[(*word_count)-1] = malloc( strlen(str_p)+1 );
if( sentence[(*word_count)-1] == NULL )
{
fprintf(stderr, "uh oh, not again! %d\n", __LINE__ );
for( i=0; i< (*word_count)-1; i++ )
{
free( sentence );
sentence = NULL;
}
return 1;
}
memcpy( sentence[(*word_count)-1], str_p,
strlen(str_p)+1 );
}
return 0;
}
=============================================
I was hoping to get some feedback on the following code.
As far as I can tell, the program works fine, but I'm not
sure I'm cleaning all the memory up properly, and I think
the "tokenize_my_string" function can probably be made better.
This program prompts the user for a text sentence,
then re-orders the words in a random fashion...
======================================================
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#define DEBUG 0
#define MAX_NOF_TOK 256 /* max number of words */
#define MAX_TOK_LEN 256 /* max word length */
#define MAX_MSG_LEN (MAX_NOF_TOK * MAX_TOK_LEN)
#define RAND_FACTOR 5
void sort_my_string( char** sentence, int word_count );
int tokenize_my_string( char* buffer,
char** sentence,
int* word_count );
int main(void)
{
char buffer[ MAX_MSG_LEN ];
char *sentence[ MAX_NOF_TOK ];
char *first_string;
int i=0, word_count=0;
/*
** output instructions
*/
fprintf( stdout, "Type in the text:\n\t" );
fflush( stdout );
/*
** get input string from user
*/
if( fgets( buffer, MAX_MSG_LEN, stdin ) == NULL )
{
fprintf(stderr, "learn how to type jackass!\n" );
return EXIT_FAILURE;
}
/*
** store buffer so strtok doesn't walk all over it
*/
first_string = malloc(strlen(buffer)+1);
if( first_string == NULL )
{
fprintf(stderr, "you need more memory dude\n");
return EXIT_FAILURE;
}
memcpy( first_string, buffer, strlen(buffer)+1 );
/*
** pesky newline...
*/
if( buffer[strlen(buffer)-1] == '\n' )
{
buffer[strlen(buffer)-1] = '\0';
}
/*
** break up the string into words
** and store each word as an element
** in the "sentence" array
**
** remember that this allocates memory for "sentence",
** so don't forget to free() it!!!
*/
if( tokenize_my_string( buffer, sentence, &word_count ) )
{
fprintf(stderr, "tokenize_my_string failed!\n" );
free( first_string );
first_string = NULL;
return EXIT_FAILURE;
}
sort_my_string( sentence, word_count );
fprintf(stdout,"randomized string looks like:\n\t");
/*
** print the sorted string
*/
for( i=0; i<word_count; i++ )
{
fprintf(stdout, "%s ", sentence );
}
fprintf(stdout, "\n");
/*
** clean up memory
*/
for( i=0; i<word_count; i++ )
{
free( sentence );
sentence = NULL;
}
free( first_string );
first_string = NULL;
return EXIT_SUCCESS;
}
void sort_my_string( char** s, int word_count )
{
char *tmp;
int ind_from, ind_to;
int rand_ind = (word_count * RAND_FACTOR);
srand( time(0) );
while( --rand_ind > 0 )
{
ind_from = rand() % word_count;
ind_to = rand() % word_count;
if( ind_from == ind_to )
continue;
tmp = s[ind_to];
s[ind_to] = s[ind_from];
s[ind_from] = tmp;
}
return;
}
int tokenize_my_string( char* buffer,
char** sentence,
int *word_count )
{
char *str_p;
int i;
/*
** get the first token...
*/
if( (str_p = strtok( buffer, " " )) == NULL )
{
fprintf( stderr, "it broke here: %d\n", __LINE__ );
return 1;
}
(*word_count) = 1;
/*
** get space for the first token in our sorted string
*/
sentence[0] = malloc( strlen(str_p)+1 );
if( sentence[0] == NULL )
{
fprintf(stderr, "malloc failed: %d\n", __LINE__ );
return 1;
}
memcpy( sentence[0], str_p, strlen(str_p)+1 );
/*
** get the rest of the tokens
*/
while( ((str_p = strtok(NULL, " "))!=NULL) &&
(++(*word_count) < MAX_NOF_TOK) )
{
/*
** make some room
*/
sentence[(*word_count)-1] = malloc( strlen(str_p)+1 );
if( sentence[(*word_count)-1] == NULL )
{
fprintf(stderr, "uh oh, not again! %d\n", __LINE__ );
for( i=0; i< (*word_count)-1; i++ )
{
free( sentence );
sentence = NULL;
}
return 1;
}
memcpy( sentence[(*word_count)-1], str_p,
strlen(str_p)+1 );
}
return 0;
}
=============================================