I've been trying to convert some code into a function. The function would receive the user input stored in a pointer to char array allocated in dynamic memory, a pointer to an array of chars (possible inaccuracy of terms) for a list of words in an array, and an integer for the number of words in the array.
char * user_input = newchar [10];
cin>> user_input;
int words_count = 100; //100 words in the array
for(int A = 0 ; A<=words_count - 1; A++)
{
int w = 0;
for(int B = 0 ; B<= strlen(words[A]) - 1; B++)
{ for(int C = 0 ; C<= strlen(user_input) - 1; C++)
{
if (words[A][B] == user_input[C])
{
// you can put cout 1 here to show all the times a letter matched
w++;
// therefor at x = 0 w != 0 but w = 1 in the for loop
// after first instance of w+1 move on to next letter in
// order to prevent w+1 on the same character
}
if (w == strlen(words[A]))
{
cout<< words[A] << endl;
break;
}
} // end of C for loop
} // end of B for loop
} // end of A for loop
delete [] words;
delete [] user_input;
When I tried converting the code above to a function of its own the function looked like this
void find_words(char words[], int words_count, char user_input[])
{
for(int A = 0 ; A<=words_count - 1; A++)
{
int w = 0;
for(int B = 0 ; B<= strlen(words[A]) - 1; B++)
{ for(int C = 0 ; C<= strlen(user_input) - 1; C++)
{
if (words[A][B] == user_input[C])
{
// you can put cout 1 here to show all the times a letter matched
w++; // therefor at x = 0 w != 0 but w = 1 in the for loop
// after first instance of w+1 move on to next letter in order to prevent w+1 on the same character
}
if (w == strlen(words[A]))
{
cout<< words[A] << endl;
break;
}
} // end of C for loop
} // end of B for loop
} // end of A for loop
delete [] words;
delete [] user_input;
}
But I kept getting compiler errors about not being able to convert type char to type const char and invalid types char[int] as subscripts. The functions compare each character in each string in the array with each character in the input by the user.
Thanks for your help writetonsharma, but I find your suggestions strange.
See, the code works fine in the main function, but when I try to make a function out of the code I get those errors. I used [A][B] to access a single character in an element (string). if words[0] was "dog", words[0][0] is 'd' .
words is defined as
char * words[3] = {"dog", "cat","horse"}; // for example
oh..ok thats what i was asking how words is defined.
Then there should not be any error in the code, all the above corrects are wrong.
to call your new function you can do this way: void find_words(char **words, int words_count, char user_input[])
as words is a double pointer and you are passing it as a single dimensional array its giving error.
secondly you dont have to delete as you haven't done any new on it.
so remove line 29 and 30.
Thanks. Your first function prototype worked like a charm. Is char **words a pointer to a char pointer? Therefore, char **words takes the address of the pointer words?
now here ptr2 points to ptr1 and ptr1 points to value. its like a chain you see ptr2 stores the address of ptr1 while ptr1 stores the address of value so value an be accessed using ptr2 with the help of ptr1.