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;
}