PDA

View Full Version : Does anybody have C programming experience?



Shade
12-09-2008, 08:18 PM
I'm having some trouble with this algorithm:

http://www.cs.iupui.edu/~n305/fall08/labs/Project7NameGame.html

Any help anyone could toss my way would be greatly appreciated. This doesn't seem like it should be that difficult, but I'm mostly having issues with splitting the string. :confused:

heywoode
12-09-2008, 10:44 PM
Sorry dude.

I've been laid.

Can't help you.


I feel for you though. Please don't be mad that I'm making light of your plight. (I'm a poet and didn't know it.)

I'm just trying to get you in a better mood!

Shade
12-09-2008, 10:46 PM
That's okay, woodey. Once I saw that you were the one who posted, any hope I had that I would get any help went right out the window.

SoupIsGood
12-10-2008, 12:56 AM
I haven't programmed in 9 years, so take this for like way less than it's even worth, but... must the string be split? Seems like you could do an i++ thing and then use i to point where you need to in the array, and copy/edit it as needed? I'm probably talking outta my ***, ummm, something like if name[0] != 'b' then you do name[0] = 'b' and if name[0] = 'b' then do name[0] = ' ' (and then m, f, although you'd probably need to have these changes each be different variables so that you then printf them, which I don't even know it that's something you can do.) Does that even make sense?

Blargh. I have no idea. I thought you were done w/ classes?

Raskolnikov
12-10-2008, 10:57 AM
Interesting problem. I program in C. I'm guessing you're using the string.h library for this? http://www.cplusplus.com/reference/clibrary/cstring/

If not, you should. PM me your code if you have some specific problem.

Raskolnikov
12-10-2008, 12:26 PM
Btw you can split a string with strcpy, well, that is to say get the rest of a string starting from a given point. Just give in (the address of) the starting point of your split as the source string. With strncpy, you can cut out any part of a string you want by adding the number of characters.

Shade
12-10-2008, 01:57 PM
Yeah, I'm using string.h, and I'm trying to use strncpy, but I'm really not all that knowledgable about mullti-dimensional arrays or pointers.

Here's what I have so far:



#include <STDIO.H>
#include <STRING.H>
int main()
{
char select;
char name;
printf ("%s\n", "1. Name Game", "2. Exit");
scanf("%s\n", &select);
printf("%s\n", "Please enter a first name: ");
scanf("%s\n", &name);
char str1[] = "&name";
char str2[1];
strcopy (str2, str1);
printf ("%s\n", "&name!\n
&name, &name, bo Bstr1\n
Bonana fanna fo Fstr1\n
fee fi mo M&srt1, &name!");
return 0;
}

I honestly have no idea if I'm even on the right track here.

ChicagoJ
12-10-2008, 07:48 PM
I took a class a couple of decades ago on Borland's C++.

My professor told me I was programming in C-. By the end of the semester, I had fallen to D+.

There is no punchline here. My GPA was the punching bag, however, and it was time to change majors...

RoboDoug
12-10-2008, 11:13 PM
#include <STDIO.H>
#include <STRING.H>
int main()
{
char select;
char name;
printf ("%s\n", "1. Name Game", "2. Exit");
scanf("%s\n", &select);
printf("%s\n", "Please enter a first name: ");
scanf("%s\n", &name);
char str1[] = "&name";
char str2[1];
strcopy (str2, str1);
printf ("%s\n", "&name!\n
&name, &name, bo Bstr1\n
Bonana fanna fo Fstr1\n
fee fi mo M&srt1, &name!");
return 0;
}


Segmentation fault

Doug
12-10-2008, 11:15 PM
Seriously, C is one of my favorites. I haven't used it in years, though. I kinda miss it.

I'm busy (and tired) right now, but I'll take a look later if I get a chance.

Shade
12-10-2008, 11:46 PM
Hmm...my #include statements don't show up on the board for some weird reason. They should be stdio.h, and string.h.<stdio.h><stdio.h><string.h>

<string.h> strcopy should condense the string down to the first digit of the name, but I don't know how to get it split properly otherwise.</string.h></string.h></stdio.h></stdio.h>

Robertmto
12-10-2008, 11:52 PM
my brain hurts

Shade
12-10-2008, 11:54 PM
Then imagine how I feel right now. :dead:

Raskolnikov
12-11-2008, 06:58 AM
Yes, definitely segmentation fault. C works with memory allocation, that is you have to specify in advance how much memory you need. The easiest way to do this here for your strings is with a fixed number of characters, say char name[200]. Also, you're gonna need strcat to form the rhyme names. Plus strpbrk is a useful function to locate a character in a string. I think these are the main functions you need: strpbrk to find the first vowel if the word doesn't start with a B, F or M, strcpy to get the tail of the splitted string, and strcat to form the rhyme names.

If you want, I'll post my solution later. Maybe tomorrow, probably Monday.

Doug
12-11-2008, 05:16 PM
Just be careful...

http://imgs.xkcd.com/comics/compiler_complaint.png (http://imgs.xkcd.com/comics/compiler_complaint.png)


http://xkcd.com/ (http://xkcd.com/371/)

Raskolnikov
12-15-2008, 10:06 AM
Here's my solution. I'm sure it could be done more efficiently, but as far I know, it works. My unreadable libraries are stdio, string and ctype. The latter you need for the function tolower.

PS You should play the game with Bart. It's fo funny.



#include <stdio.h>
#include <string.h>
#include <ctype.h>

/* List of functions */
void namegame(char* name);
char* get_rhyme(char* name,char* rhyme,char a);
int first_vowel(char* string);

/* Main of the program. Loops over the namegame until the user exits. */
int main(void)
{
int choice;
char name[200];

do{
printf("1.Name Game, 2.Exit.\n");
scanf("%d",&choice);

if(choice==1)
{
printf("Enter a name.\n");
scanf("%s",name);
namegame(name);
}
}
while(choice==1);
}

/* Play the namegame */
void namegame(char* name)
{
char rhyme[200];

printf("%s!\n",name);
/* First B */
get_rhyme(name,rhyme,'B');
printf("%s, %s %s Bonana fanna ",name,name,rhyme);
/* Then F */
get_rhyme(name,rhyme,'F');
printf("%s\n",rhyme);
/* Then M */
get_rhyme(name,rhyme,'M');
printf("Fee fy %s, %s!\n",rhyme,name);
}

/* Gives the rhyme for a given name and character,
* including the preceeding o-word.
* For instance, get_rhyme("Fred",'F') returns "Fo red"
*/
char* get_rhyme(char* name,char* rhyme,char a)
{
char temp[200]={};
char cut[200];
int cutoff;
char b;

/* First letter is equal to a */
if(name[0] == a)
{
cutoff = 1;
b = a;
}
/* General case */
else{
cutoff = first_vowel(name);
b = tolower(a);
}

/* o-word */
temp[0] = b;
strcat(temp,"o ");
/* Actual rhyme */
if(name[0] != a)
temp[3] = a;
strcat(temp,strcpy(cut,&name[cutoff]));

strcpy(rhyme,temp);
}

/* Returns position of the first vowel in a string */
int first_vowel(char* string)
{
char* vowels = "aeiou";
return strcspn(string,vowels);
}

</ctype.h></string.h></stdio.h>