I am attempting to tokenize and store separate words into an array but for some reason the first word gets stored in index 0 and the rest of the words are not tokenized and are all stored in index 1.
I have the following code....
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int main(int argc, char const *argv[])
{
char input[300];
while (1)
{
printf ("\nEnter input to check or q to quit\n");
fgets(input, 300, stdin);
for (int j = 0; j < 300; j++)
{
input[j] = tolower(input[j]);
}
/* remove the newline character from the input */
int i = 0;
while (input[i] != '\n' && input[i] != '\0')
{
i++;
}
input[i] = '\0';
/* check if user enter q or Q to quit program */
if ( (strcmp (input, "q") == 0) || (strcmp (input, "Q") == 0) )
break;
//printf ("%s\n", input);
/*Start tokenizing the input into words separated by space
*The tokenized words are added to an array of words*/
char delim[] = " ";
char *ptr = strtok(input, delim);
int j = 0 ;
// allocate our array
char *wordList[15];
for (i = 0 ; i < 16; i++)
{
wordList[i] = (char *) malloc(sizeof(char) * 300);
}
while (ptr != NULL)
{
strcpy(wordList[j], ptr);
printf ("%s\n", wordList[j]);
ptr = strtok(NULL, delim);
j++;
}
printf ("%s\n", wordList[1]);
}
printf ("\nGoodbye\n");
return 0;
}
Expected output
hello
there
sir
Output I'm getting
hello
there sir
Any help is greatly appreciated. Thanks.
This loop
// allocate our array
char *wordList[15];
for (i = 0 ; i < 16; i++)
{
wordList[i] = (char *) malloc(sizeof(char) * 300);
}
is bad because it is writing to wordList[15] while only wordList[0] to wordList[14] are available.
delim may be placed just after wordList and writing to this out-of-range wordList[15] may break the contents of delim.
Increase the number of elements
// allocate our array
char *wordList[16];
for (i = 0 ; i < 16; i++)
{
wordList[i] = (char *) malloc(sizeof(char) * 300);
}
Or decrease the number of iteration.
// allocate our array
char *wordList[15];
for (i = 0 ; i < 15; i++)
{
wordList[i] = (char *) malloc(sizeof(char) * 300);
}
Related
I have program to remove the similar words from string but this program only removing at once word not a repeating words.
For example input:
sabunkerasmaskera kera
and should an output:
sabunmas
This my code:
#include <stdio.h>
#include <string.h>
void remove(char x[100], char y[100][100], char words[100]) {
int i = 0, j = 0, k = 0;
for (i = 0; x[i] != '\0'; i++) {
if (x[i] == ' ') {
y[k][j] = '\0';
k++;
j = 0;
} else {
y[k][j] = x[i];
j++;
}
}
y[k][j] = '\0';
j = 0;
for (i = 0; i < k + 1; i++) {
if (strcmp(y[i], kata) == 0) {
y[i][j] = '\0';
}
}
j = 0;
for (i = 0; i < k + 1; i++) {
if (y[i][j] == '\0')
continue;
else
printf("%s ", y[i]);
}
printf ("\n");
}
int main() {
char x[100], y[100][100], kata[100];
printf ("Enter word:\n");
gets(x);
printf("Enter word to remove:\n");
gets(words);
remove(x, y, words);
return 0;
}
My program output its:
sabunkerasmaskerara
and that should not be the case. Maybe I need your opinion to fixed this program and also I need help to make it better.
Your solution does not work because it uses strcmp to compare the string portions, which only works if the substring is at the end of the string, as this makes it null-terminated.
You should instead use strstr to locate the matches and use memmove to shift the string contents.
There are other issues in your code:
do not use gets()
y is unnecessary for this task.
words is not defined
Here is a modified version:
#include <stdio.h>
#include <string.h>
char *remove_all(char *str, const char *word) {
size_t len = strlen(word);
if (len != 0) {
char *p = str;
while ((p = strstr(p, word)) != NULL) {
memmove(p, p + len, strlen(p + len) + 1);
}
}
return str;
}
int main() {
char str[100], word[100];
printf ("Enter string:\n");
if (!fgets(str, sizeof str, stdin))
return 1;
printf("Enter word to remove:\n");
if (!fgets(word, sizeof word, stdin))
return 1;
word[strcspn(word, "\n")] = '\0'; // strip the trailing newline if any
remove_all(str, word);
fputs(str, stdout);
return 0;
}
I am currently working on a project that reads strings from an input file and stores them into an array.
When it is stored into an Array then I want to remove the spaces so that I can compare the strings in the array with the array stringcards and check if all the Cards from the input file are there.
But I am currently stuck at storing the new strings without the space in the array and printing them out.
It prints out the first string REDAbut after that I get an Segmentation Fault.
I would also appreciate it , if someone could give me any hints on how to compare the strings in the cardarray with the constant array and check if all the cards are in the array.
I hope that it's the right approach.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
const char * stringcard[] = { "REDA","RED2"
"RED3"
"RED4"
"RED5"
"RED6"
"RED7"
"RED8"
"RED9"
"RED10"
"REDJ"
"REDQ"
"REDK"
};
int main (int argc, char **argv) {
char *reds[13];
char * cardarray[13];
int i;
FILE *file = argc > 1 ? fopen (argv[1], "r") : stdin;
if (file == NULL)
return 1;
if(argc!=2) {
printf("[ERR]");
return 0;
}
for (i =0; i < 13; i++) {
reds[i] = malloc( 8);
fgets(reds[i], 8, file);
}
int i2 = 0;
for (i =0; i < 13; i++) {
printf ("%s", reds[i]);
}
for(i= 0; i<13; i++) {
char *p = strtok (reds[i], " ");
while (p != NULL)
{
cardarray[i2++] = p;
p = strtok (NULL, " ");
}
}
for (i =0; i < 13; i++) {
printf ("%s", cardarray[i]);
}
return 0;
}
Input file:
RED A
RED 2
RED 3
RED 4
RED 5
RED 6
RED 7
RED 8
RED 9
RED 10
RED J
RED Q
RED K
Just remove the not needed char. Here you have two functions:
First algorithm is much faster. The second slower but easy to understand
#include <stdio.h>
#include <string.h>
char *removechar(char *str, int ch)
{
char *cptr = str, *readptr = str;
while(*readptr)
{
if(*readptr == ch)
{
readptr++;
}
else
{
*cptr++ = *readptr++;
}
}
*cptr = 0;
return str;
}
char *removechar(char *str, int ch)
{
char *cpos = str;
while((cpos = strchr(cpos, ch)))
{
strcpy(cpos, cpos + 1);
}
return str;
}
There are at least 2 problems:
First: In this loop you increment i twice, which leads finally to a buffer overflow.
for (i = 0; i < 13; i++) {
char *p = strtok(reds[i], " ");
while (p != NULL)
{
if (i >= 13) // debug code
{ // debug code
printf("Bummer\n"); // debug code
exit(1); // debug code
} // debug code
cardarray[i++] = p;
p = strtok(NULL, " ");
}
}
Secondly: you don't allocate enough memory here:
reds[i] = malloc(sizeof(char) * (4 + 1)); // you allocate space for 5 chars
fgets(reds[i], 13, file); // and here you tell fgets that
// your buffer has a length of 13 chars...
There are most likely more errors though.
I'm trying to split a sentence the user inputs to an array of words so I can later manipulate the words separately as strings.
The code is compiling but prints only garbage after the user input.
I tried debugging but don't see the problem. Can someone help me fix it?
#include <stdio.h>
#include <string.h>
int main() {
char str[1000];
int i = 0;
char rev[1000][1000];
int r = 0;
puts("Enter text:");
gets(str);
int k, length = 0;
printf_s("So the words are:\n");
while (str[i] != '\0') {
if (str[i] == ' ') {
k = i - length;
do {
rev[r][k] = (str[k]);
k++;
} while (str[k] != ' ');
printf(" ");
length = (-1);
r++;
} else
if (str[i + 1] == '\0') {
k = i - length;
do {
rev[r][k] = (str[k]);
k++;
} while (str[k] != '\0');
length = 0;
r++;
}
length++;
i++;
}
for (int r = 0; r < 1000; r++)
printf("%s ", rev[r]);
return 0;
}
fix like this
#include <stdio.h>
int main(void) {
char str[1000];
char rev[1000][1000];
puts("Enter text:");
fgets(str, sizeof str, stdin);//Use fgets instead of gets. It has already been abolished.
int r = 0;
int k = 0;
for(int i = 0; str[i] != '\0'; ++i){
if (str[i] == ' ' || str[i] == '\n'){//is delimiter
if(k != 0){
rev[r++][k] = '\0';//add null-terminator and increment rows
k = 0;//reset store position
}
} else {
rev[r][k++] = str[i];
}
}
if(k != 0)//Lastly there was no delimiter
rev[r++][k] = '\0';
puts("So the words are:");
for (int i = 0; i < r; i++){
printf("%s", rev[i]);
if(i < r - 2)
printf(", ");
else if(i == r - 2)
printf(" and ");
}
return 0;
}
Replace you declaration
char rev[1000][1000];
with
char * rev[1000]; // We will need pointers only
int i = 0; // Index to previous array
and all your code after
puts( "Enter text:" );
with this:
fgets( str, 998, stdin ); // Safe way; don't use gets(str)
const char delim[] = ",; "; // Possible delimiters - comma, semicolon, space
char *word;
/* Get the first word */
word = strtok( str, delim );
rev[i++] = word;
/* Get the next words */
while( word != NULL )
{
word = strtok( NULL, delim );
rev[i++] = word;
}
/* Testing */
for (int r = 0; r < i - 1; r++)
printf( "%s\n", rev[r] );
return 0
}
As you can see, all dirty work is done with the strtok() function ("string to tokens") which walks through other and other words ("tokens"), recognizing them as delimited by one or more characters from the string delim.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int count_spaces(char *str)
{
if (str == NULL || strlen(str) <= 0)
return (0);
int i = 0, count = 0;
while (str[i])
{
if (str[i] == ' ')
count++;
i++;
}
return (count);
}
int count_char_from_pos(char *str, int pos)
{
if (str == NULL || strlen(str) <= 0)
return 0;
int i = pos, count = 0;
while (str[i] && str[i] != ' ')
{
count++;
i++;
}
return count;
}
char **get_words(char *str)
{
if (str == NULL || strlen(str) <= 0)
{
printf("Bad string inputed");
return NULL;
}
int i = 0, j = 0, k = 0;
char **dest;
if ((dest = malloc(sizeof(char*) * (count_spaces(str) + 1))) == NULL
|| (dest[0] = malloc(sizeof(char) * (count_char_from_pos(str, 0) + 1))) == NULL)
{
printf("Malloc failed\n");
return NULL;
}
while (str[i])
{
if (str[i] == ' ') {
dest[j++][k] = '\0';
if ((dest[j] = malloc(sizeof(char) * (count_char_from_pos(str, i) + 1))) == NULL)
{
printf("Malloc failed\n");
return NULL;
}
k = 0;
}
else {
dest[j][k++] = str[i];
}
i++;
}
dest[j][k] = 0;
dest[j + 1] = NULL;
return dest;
}
int main(void) {
char *line = NULL;
size_t n = 0;
getline(&line, &n, stdin);
printf("%s\n", line);
line[strlen(line) - 1] = 0;
printf("%s\n", line);
char **tab = get_words(line);
int i = 0;
while (tab[i])
{
printf("%s\n", tab[i++]);
}
}
here is a long but fully working example
get the user input
then send it to get_words function. It will get the number of words, the number of characters for each words, allocate everything in memory and writes chars then return it. You get a char ** and prints it just tested it it works
If you wish to split a string into an array of strings, you should consider the strtok function from #include <string.h>. The strtok function will the split the string on the given delimiter(s). For your case, it would the " ".
Using the strtok example from Tutorials Point:
#include <string.h>
#include <stdio.h>
int main(){
char str[80] = "This is - www.tutorialspoint.com - website";//The string you wish to split
const char s[] = "-";//The thing you want it to split from. But there is no need to this.
char *token;//Storing the string
/* get the first token */
token = strtok(str, s);//Split str one time using the delimiter s
/* walk through other tokens */
while( token != NULL )
{
printf( " %s\n", token );//Print the string
token = strtok(NULL, s);//Split the string again using the delimiter
}
return(0);
}
I have a problem with the following program.
The main function calls the function returnArrayOfWords(arrS1, &ptrArray1) twice. On the first call, the array is parsed perfectly, and afterward the pointer always points to the first word. On the other hand, after the second call, the pointer of the first array points to the second word, the third word, or sometimes the first word, but it should always point to the first word -- nowhere else.
Why does the function misbehave when called for the second time?
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void returnArrayOfWords (char *str4Parsing, char *arrayParsed[])
{
char seps[] = " ,\t\n"; // separators
char *token1 = NULL;
char *next_token1 = NULL;
int i = 0;
// Establish string and get the first token:
token1 = strtok_s( str4Parsing, seps, &next_token1);
// While there are tokens in "str4Parsing"
while (token1 != NULL)
{
// Get next token:
if (token1 != NULL)
{
arrayParsed[i] = token1;
printf( " %s\n", token1 );
token1 = strtok_s( NULL, seps, &next_token1);
i++;
}
}
}
//int main1 ()
int main ()
{
int i, j, n = 80; /*max number of words in string*/
char arrS1[80], arrS2[80];
const char *w1, *w2; /*pointers*/
char *ptrArray1, *ptrArray2;
int currLength1 = 0, currLength2 = 0 ;
int sizeArr1 = 0, sizeArr2 = 0;
int maxLength = 0;
char wordMaxLength ;
printf("Type your first string: ");
fgets(arrS1, 80, stdin);
returnArrayOfWords(arrS1, &ptrArray1);
sizeArr1 = sizeof(ptrArray1) / sizeof(ptrArray1[0]);
printf("Type your second string: ");
fgets(arrS2, 80, stdin);
returnArrayOfWords(arrS2, &ptrArray2);
sizeArr2 = sizeof(ptrArray2) / sizeof(ptrArray2[0]);
for (i = 0; i < sizeArr1; i++)
{
// to find the largest word in the array
w1 = &ptrArray1[i];
currLength1 = strlen(w1);
for (j = 0; j < sizeArr2; j++)
{
w2 = &ptrArray2[j];
currLength2 = strlen(w2);
if (strcoll(w1, w2) == 0)
// compares the strings
{
if (currLength2 >= maxLength)
// in the 0th element -> the length of the longest word
{
maxLength = currLength2;
wordMaxLength = ptrArray2[j];
}
}
}
}
printf("The largest word is: %s", wordMaxLength);
return 0;
}
EDIT:
Here's the latest version of the code, everything here works fine, managed to fix it myself. I'm just posting it in case somebody needs it as a solution:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stddef.h>
#include <ctype.h>
#define n 80 /*max number of words in string*/
/* Arrays and pointers */
int returnArrayOfWords (char *str4Parsing, char *arrayParsed[])
{
// returns the length of array
int elArr = 0, na = 0;
char *delim = " .,;-\t\n"; /* word delimiters */
char *next_token1 = NULL;
char *ap = str4Parsing; /* pointer to str4Parsing */
for (ap = strtok_s (str4Parsing, delim, &next_token1); ap; ap = strtok_s( NULL, delim, &next_token1))
{
arrayParsed[na++] = ap;
elArr++;
}
return elArr;
}
void printArr(char *arr[])
{
int i;
for ( i = 0; i < n; i++)
{
printf("Element %d is %s \n", i, arr[i]);
}
}
void findLargestWord(char *ptrArray1[], int sizeArr1, char *ptrArray2[], int sizeArr2)
{
size_t maxLength = 0;
char *wordMaxLength = NULL ;
int i = 0, j = 0;
char *w1 = NULL, *w2 = NULL; /*pointers*/
size_t currLength1 = 0, currLength2 = 0 ;
for (i = 0; i < sizeArr1; i++)
{
// to find the largest word in the array
w1 = (ptrArray1[i]); // value of address (ptrArray1 + i)
currLength1 = strlen(w1);
//printf("The word from the first string is: %s and its length is : %d \n", w1, currLength1); // check point
for (j = 0; j < sizeArr2; j++)
{
w2 = (ptrArray2[j]); // value of address (ptrArray2 + j)
currLength2 = strlen(w2);
//printf("The word from the second string is : %s and its length is : %d \n", w2, currLength2); // check point
if (strcoll(w1, w2) == 0 && currLength1 == currLength2)
// compares the strings
{
if (currLength2 >= maxLength)
// in the variable maxLength -> the length of the longest word
{
maxLength = currLength2;
wordMaxLength = w2;
//printf("The largest word for now is : %s and its length is : %d \n", wordMaxLength, maxLength); // check point
}
}
}
}
printf("The largest word is: %s \n", wordMaxLength);
printf("Its length is: %d \n", maxLength);
}
void typeArray (char *arrS1)
{
int err = 0;
if (!fgets (arrS1, n, stdin)) { /* validate 'arrS1' */
fprintf (stderr, "Error: invalid input for string.\n");
err = 1;
}
while (err == 1)
{
if (!fgets (arrS1, n, stdin)) { /* validate 'arrS1' */
fprintf (stderr, "Error: invalid input for string.\n");
err = 1;
}
}
}
int main(void)
{
char arrS1[n], arrS2[n];
char *ptrArray1[n] = {NULL}, *ptrArray2[n] = {NULL};
int sizeArr1 = 0, sizeArr2 = 0;
printf("Type your first string: ");
typeArray (arrS1);
sizeArr1 = returnArrayOfWords (arrS1, ptrArray1); // sizeArr1 = number of elements in array 1
printf("Type your second string: ");
typeArray (arrS2);
sizeArr2 = returnArrayOfWords (arrS2, ptrArray2); // sizeArr2 = number of elements in array 2
findLargestWord(ptrArray1, sizeArr1, ptrArray2, sizeArr2);
return 0;
}
There are numerous errors in the program although it compiled without any warnings. Chiefly the pointer types for your array, and the memory allocated. Secondly the function does not know how many words is allowed, and does not return how many were read - your method did not work at all (as in comments). Thirdly the string comparisons: you did not state the goals clearly, but in comment you want the "biggest string". strcoll does not do that - it's a lexical comparison, so I changed that section to find the longest string for the two sentences you enter. See comments, I made a large number of changes.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int returnArrayOfWords (char *str4Parsing, char *arrayParsed[], int maxtokens) // added max
{
char seps[] = " ,\t\n"; // separators
char *token1 = NULL;
char *next_token1 = NULL;
int i = 0;
// Establish string and get the first token:
token1 = strtok_s( str4Parsing, seps, &next_token1);
// While there are tokens in "str4Parsing"
while (token1 != NULL)
{
if(i >= maxtokens)
return i; // ignore the rest
arrayParsed[i] = token1;
printf( " %s\n", token1 );
token1 = strtok_s( NULL, seps, &next_token1);
i++;
}
return i;
}
int main (void) // correct signature
{
int i, j, n = 80; /*max number of words in string*/
char arrS1[80], arrS2[80];
//const char *w1, *w2; /*pointers*/ // deleted
char **ptrArray1, **ptrArray2; // changed type
int currLength1 = 0, currLength2 = 0 ;
int sizeArr1 = 0, sizeArr2 = 0;
int maxLength = 0;
char *wordMaxLength; // changed to pointer
ptrArray1 = malloc(n * sizeof (char*)); // allocate mem for pointer array
if (ptrArray1 == NULL)
return 1;
ptrArray2 = malloc(n * sizeof (char*)); // allocate mem for pointer array
if (ptrArray2 == NULL)
return 1;
printf("Type your first string: ");
fgets(arrS1, 80, stdin);
sizeArr1 = returnArrayOfWords(arrS1, ptrArray1, n); // indirection error, added max words, get actual num
printf("Type your second string: ");
fgets(arrS2, 80, stdin);
sizeArr2 = returnArrayOfWords(arrS2, ptrArray2, n); // indirection error, added max words, get actual num
for (i = 0; i < sizeArr1; i++) // this section rewritten
{
// to find the largest word in the array
currLength1 = strlen(ptrArray1[i]);
if(currLength1 > maxLength)
{
maxLength = currLength1;
wordMaxLength = ptrArray1[i]; // changed definition to pointer
}
}
for (j = 0; j < sizeArr2; j++)
{
// to find the largest word in the array
currLength2 = strlen(ptrArray2[j]);
if(currLength2 > maxLength)
{
maxLength = currLength2;
wordMaxLength = ptrArray2[j]; // changed definition to pointer
}
}
printf("The largest word is: %s", wordMaxLength);
free(ptrArray1); // added
free(ptrArray2);
return 0;
}
Program session:
Type your first string: one two three four
one
two
three
four
Type your second string: apple banana pear
apple
banana
pear
The largest word is: banana
So basically what my program did before i had to change it so that it would accept arbitrary values, was to take x-amount of words and the size of the words would also be arbitrary. (both are user inputted). I did this via a multiArray.
Then sorted according to alphabetical-order.
I'm just going to put it out there as my code is shit and I'm very unfamiliar with the usage of arbitrary-strings and pointers. I've read up on it in the manual but the concept needs to sink in a little bit first i believe. Anyhow, I get the error: "Abort trap: 6" when i run the program. Could anyone please help me fix this problem so that i can see how the code would look like if it was actually working, i think that would help me understand both pointers and allocating memory a lot better. Forever in debt if you do.
Current code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX_LENGTH 10
int main(){ //8
char *name;
char tname[] = {0};
char temp[] = {0};
int i=0, j=0, n=0;
ssize_t bytes_read;
size_t bytes_number;
printf("Enter the amount of words you want to input: ");
scanf("%d", &n);
printf("Enter %d words: ",n);
bytes_number = MAX_LENGTH;
name = (char *) malloc (bytes_number+ 1);
bytes_number = 0;
bytes_read = getline(&name, &bytes_number, stdin);
if (bytes_read == -1){
puts("ERROR!");
free(name);
}
for (i = 0; i < n; i++){
strcpy(&tname[i], &name[i]);
}
for (i = 0; i < n - 1 ; i++){
for ( j = i + 1; j < n; j++){
if (strcmp(&name[i], &name[j]) > 0){
strcpy(temp, &name[i]);
strcpy(&name[i], &name[j]);
strcpy(&name[j], temp);
}
}
}
printf("\n------------------------------------------\n");
printf("%-3s %4s %11s\n", "Input","|", "Output");
printf("------------------------------------------\n");
for (i = 0; i < n; i++)
{
printf("%s\t\t%s\n", &tname[i], &name[i]);
}
printf("------------------------------------------\n");
}
This
strcpy(&tname[i], &name[i]);
is completely wrong, if you just want to copy all the characters, then it's just
strcpy(tname, name);
which is equivalent to
for (size_t i = 0 ; name[i] != '\0' ; ++i)
tname[i] = name[i];
using strcpy(&tname[i], &name[i]) is wrong because it will copy all the bytes from name until '\0' is found, at every loop starting at the i-th character.
But this will fail again because tname is does not have room, it's an array with just one element.
Since you want to sort the strings, you DO NOT NEED TO COPY them. Just swap the pointers. Also
char temp[] = {0};
only allocates 1 character, thus
strcpy(temp, name);
will invoke Undefined Behavior.
Try this, maybe it's what you need
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int
main(void)
{
char **words;
char *temp;
int word_count;
int actual_count;
char *word;
size_t length;
int result;
printf("Enter the amount of words you want to input: ");
if (scanf("%d%*c", &word_count) != 1)
return -1; // Input error
printf("Enter '%d' words:\n", word_count);
words = NULL;
word = NULL;
result = -1;
actual_count = 0;
length = 0;
for (int i = 0 ; i < word_count ; ++i)
{
char **pointer;
printf("Word(%d) > ", i + 1);
if ((length = getline(&word, &length, stdin)) <= 0)
goto cleanup;
// Grow the array of words
pointer = realloc(words, (i + 1) * sizeof(*pointer));
if (pointer == NULL)
goto cleanup; // Memory Exhausted
// Now it's safe to overwrite `words'
words = pointer;
words[i] = malloc(length);
if (words[i] == NULL)
goto cleanup; // Memory Exhausted
memcpy(words[i], word, length);
words[i][length - 1] = '\0'; // Replace '\n' with '\0'
actual_count += 1;
}
printf("Input : ");
for (int i = 0 ; i < actual_count ; ++i)
printf("%s\t", words[i]);
printf("\n");
for (int i = 0; i < actual_count - 1 ; i++)
{
for (int j = i + 1 ; j < actual_count ; ++j)
{
if (strcmp(words[i], words[j]) <= 0)
continue;
temp = words[i];
words[i] = words[j];
words[j] = temp;
}
}
printf("Output: ");
for (int i = 0 ; i < actual_count ; ++i)
printf("%s\t", words[i]);
printf("\n");
result = 0;
cleanup:
free(word);
for (int i = 0; i < actual_count ; i++)
free(words[i]);
free(words);
return result;
}
Note: This would consider an empty word (made completely of white space characters), as a valid word.