C/string handling problem
Expert: Zlatko - 7/8/2011
QuestionQUESTION: i have a question to solve....where i am suppose to enter n number of set of names and then m number of set of names...and then i am suppose to compare them and say how many of the names match.
for example :
input will be-
2
jack jill brad flora
3
kim jack brad
output will be-
2
please help...thank you
ANSWER: Hello Refaya
You have not really told me what part you are having trouble with. I am happy to help you but I cannot do it all for you. Perhaps you need some help to start.
The first thing you need to do is get the count of names for the first list. You can get that using the scanf function. You can see how to use scanf here:
http://www.cplusplus.com/reference/clibrary/cstdio/scanf/
The next thing you will need is a loop to read in the names "count" times. Inside the loop you will need to read in one name and store it somewhere. Lets start by keeping things simple. You can store the names on a two dimensional array. For example, you can declare an array like this:
char nameList[20][15];
That is an array which can hold 20 names, and each name can have at most 15 characters. You can make the length or width bigger if you like. You can scanf the i'th name directly into the array like this:
scanf("%s", nameList[i]);
Try making a loop using i as the counter and scanf names into the name list. Then make another loop to print the names out with
printf("%s\n", nameList[i]);
Show me some of your effort and I will help you more by telling you the next steps. If you have problems compiling, show me your code and I will help you to compile it.
Good luck and best regards
Zlatko
---------- FOLLOW-UP ----------
QUESTION: here's the program
#include <stdio.h>
int main()
{
int i,j,r,k;
printf ("how many names do u want to enter for set 1?\n");
scanf("%d", &r);
char name1[r][20];
for (i=0;i<r;i++)
{
printf ("enter name %d: ", i);
scanf ("%s", name1[i]);
printf ("\n");
}
printf ("how many names do u want to enter for set 2?\n");
scanf("%d", &k);
char name2[k][20];
for (i=0;i<k;i++)
{
printf ("enter name %d: ", i);
scanf ("%s", name2[i]);
printf ("\n");
}
printf ("\nSet 1: ");
for (i=0;i<r;i++)
{
printf ("%s ", name1[i]);
}
printf ("\nSet 2: ");
for (i=0;i<k;i++)
{
printf ("%s ", name2[i]);
}
return 0;
}
it worked...thank u....but now how do i compare the two sets of names to see how many names match?
AnswerHello Refaya.
That is great! You must be using the GNU gcc compiler. I have read that the latest C standard allows for variables to be used when declaring arrays, but my usual compiler, from Microsoft, does not support it. That's fine, because the code compiles with gcc.
At this point, your name arrays are unsorted. To find the intersection of the sets, you must go through each item in name1, and check if it exists in name2. You will need two nested loops to do that. The outer loop will have a counter i, going from 0 to <r, and the inner will count j from 0 to <k. You can use the strcmp to compare the names like this:
if (strcmp(name1[i], name2[j]) == 0)
{
/* then the names are equal
you can print the name and break out of the inner loop
*/
}
How efficient is this method? For each entry in name1 it has to examine each entry in name2. If there are M entries in name1 and N entries in name2, then the running time of the intersection check is N*M. This is not the actual time in seconds, but it is a way to think about the efficiency of an algorithm. If the name arrays were sorted, we could make a more efficient algorithm, one which has a running time of N+M, instead of N*M. If you take more advanced courses you will learn more about making efficient algorithms.
For now, don't worry about it unless you are really interested or your instructor suggests it.
I have a few suggestions for your program.
1) You use more descriptive variable names. For example, k should be renamed to show that it is the size of the name2 array. name2Size would be a good name for k. It is too easy to get i,j, and k mixed up.
2) When using scanf for numbers, it is important to check if the user actually entered a number. When the program asks you to enter how many names you want for set 1, and you enter "asdf", what happens?
The scanf will return the number of items it successfully read, so when reading %d, you need to check that scanf read 1 number by checking that scanf returns 1. If scanf does not return 1, then the user entered something wrong, and you need to erase what the user entered or else it will interfere with the next scanf. Below is a function which you can use to ensure that the user entered a number.
int getInt(const char* prompt)
{
int result;
printf("%s\n", prompt);
while (scanf("%d", &result) != 1)
{
clearKeyboardBuffer();
printf("Try again\n");
printf("%s\n", prompt);
}
clearKeyboardBuffer();
return result;
}
Here is a sample of how to use it. Use the program below and see if you can enter any bad input.
int main()
{
int r;
while (1)
{
r = getInt("how many names do u want to enter for set 1?\n");
printf("Got Int %d\n", r);
}
return 0;
}
You can use the following function to clear the keyboard buffer to get rid of any bad input from the user.
void clearKeyboardBuffer()
{
/* don't use fflush(stdin) to clear the keyboard buffer
it is not correct by the C standard and will not work
with all compilers
*/
while(getc(stdin) != '\n');
}
Show me the next version of your program, and I will give you more help.