LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 04-18-2011, 08:49 PM   #1
SithArrow
LQ Newbie
 
Registered: Apr 2011
Posts: 3

Rep: Reputation: 0
Structures and String Arrays, C


I am working on a project that needs to use structures and I'm pretty sure string arrays. First I declare my structures and they must be exactly like this.

typedef struct
{
int hour;
int minute;
char ampm[3];
} time_type;

typedef struct
{
char ride_name[MAX_SIZE];
time_t ride_time;
} fastpass_type;

typedef struct
{
char first_name[MAX_SIZE];
char last_name[MAX_SIZE];
fastpass_type passes[MAX_PASSES];
int num_passes;
} customer_type;

There i read in various names from a file. I am not sure how to assign the name to a spot in the array. Am I supposed to declare
time_type time;
fastpass_type ride1;
customer_type person1;
for example? But then would I have to do that for every person because I don't know how many there are. I have a for loop that scans them in.

Also I must take each of these names and change the lettering around to determine if they are like the other names.

Any help or general info would be much appreciated. Thank you in advance.
 
Old 04-18-2011, 09:59 PM   #2
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,151

Rep: Reputation: 331Reputation: 331Reputation: 331Reputation: 331
Try something like this:
Code:
times = (time_type *) malloc(100 * sizeof(time_type)); #etc.
# The "100" is arbitrary. You can always realloc() if you need more than 100 "people".
but a better solution would be to use, say, sqlite to create a data base containing your input data in tables, and then have your C program access the tables with the appropriate queries. (I suggest sqlite since it's designed to be created and called from C programs. F.Y.I.: Firefox, for example, uses sqlite for much of its internal housekeeping.)

By the way, the "fuzzy matching" of names to find duplicates is a non-trivial task. Much has been written about such matching (often in the context of "cleaning" mailing lists of duplicated names), and I'd suggest you investigate the literature before "jumping into the deep end of the pool." Without additional information, an exactly matched name is, of course, just that, with no clue if it's the same person or not. (I know of one Canadian athlete with the same name as mine. Google your own name sometime if you think your name is unique.)
 
Old 04-18-2011, 10:04 PM   #3
Shum
LQ Newbie
 
Registered: Oct 2007
Posts: 23

Rep: Reputation: 0
Your main function can declare an array of people the same way you're declaring an array of fastpass_types inside you're customer_type struct.

Code:
customer_type people[MAX_NUMBER_OF_PEOPLE];
Then you can refer to individual people by indexing that array.

Code:
people[0].num_passes = 23;
people[1].num_passes = 73;
people[2].num_passes = -5;
Although you'd want to do this inside the for loop and index 'people' with a variable. I'll leave that as an exercise for you.

What do you mean by "change the lettering around to determine if they are like the other names". You'll have to be more specific.

Last edited by Shum; 04-18-2011 at 10:07 PM.
 
Old 04-18-2011, 10:13 PM   #4
SithArrow
LQ Newbie
 
Registered: Apr 2011
Posts: 3

Original Poster
Rep: Reputation: 0
Thanks. I just figured that out too lol.

For the names here is the directions. I figure I am going to have to sort through the array with different methods to test every variation of the name.

Thus, for the purposes of your program, you are to treat two names as being the same so long as both the first and last names “match.” In order for there to be a match one of the following has to be true:

1) The names are exactly the same (without regard to case).

2) The names are both exactly the same length, with all corresponding letters matching, except for one (without regard to case).

3) The two names are reversed versions of each other (without regard to case).

Here is a list of some possible versions of “Bill Johnson”:

Will Johnson (changed the ‘B’ to a ‘W’)
BiLk Johnsot (changed the ‘l’ to a ‘k’ in Bill and the ‘n’ to a ‘t’ in Johnson)
Lliw Johnton (Reversed the first name and changed the ‘s’ to a ‘t’ in Johnson)
Bill NoSnHoj (Reversed the second name)

Here are names that should NOT be counted as “Bill Johnson”:

Bill NosnhiJ (The last name is both reversed AND contains a letter change)
Bll Johnson (The first name is a different length than the last.)
Bill Jhnsono (There are 6 sets of corresponding letters in the last name that don’t match)
Wlli Johnson (There are 2 sets of corresponding letters in the first name that don’t match)
 
Old 04-18-2011, 10:32 PM   #5
Shum
LQ Newbie
 
Registered: Oct 2007
Posts: 23

Rep: Reputation: 0
If you need to test every name against every other name you'll need two for loops.

Code:
for(i = 0; i < MAX_PEOPLE; i++) {
    for(j = i + 1; j < MAX_PEOPLE; j++) {
        test people[i] vs people[j] in here.
    }
}
To compare two names you don't need to test every variation of the names. Just go through the two names character-by-character at the same time and compare how many letters are different. Then do it again except loop through one of the names in the opposite direction to test the reverse case.

Also here's a handy macro that will convert characters to upper case so you compare them easier.
Code:
#define to_upper(x) ((x) < 'a' ? (x) : (x) + 'A' - 'a')
Just chuck that at the top your code somewhere and you can use to_upper like a function. The ? : notation is just a shorthand if statement. If x is lesser than 'a' (upper-case letter are ordered before lower-case letters) then return it, otherwise shift it by however many values separate 'a' and 'A' (which will convert lower-case letters to upper-case).

Last edited by Shum; 04-18-2011 at 10:39 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Accessing arrays position in a split string xeon123 Linux - Newbie 4 03-10-2011 09:36 AM
[SOLVED] copy string a to string b and change string b with toupper() and count the chars beep3r Programming 3 10-22-2010 07:22 PM
Arrays of Structures to Arrays of Classes knobby67 Programming 1 01-01-2008 01:39 PM
Question about outputing arrays with pointers, then just arrays... RHLinuxGUY Programming 1 04-12-2006 05:40 AM
initializing arrays of structures in c dreamgoat Programming 5 09-27-2004 02:02 AM


All times are GMT -5. The time now is 03:18 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration