Xente's Profile User Rating: -----

Reputation: 2 Apprentice
Group:
Members
Active Posts:
36 (0.04 per day)
Joined:
31-January 12
Profile Views:
2,013
Last Active:
User is offline Jul 21 2013 08:53 PM
Currently:
Offline

Previous Fields

Country:
Who Cares
OS Preference:
Who Cares
Favorite Browser:
Who Cares
Favorite Processor:
Who Cares
Favorite Gaming Platform:
Who Cares
Your Car:
Who Cares
Dream Kudos:
0
Icon   Xente has not set their status

Posts I've Made

  1. In Topic: Trying to close an array of pipes in C

    Posted 5 Apr 2013

    I had a feeling that it was gonna be something stupid. I didn't notice the lack of braces. I guess that's why all my professors have always said not to be lazy and leave out the braces when there is only one statement. Thank you so much. I added the two braces and I finally got it to work. I thought something was wrong with my logic so I brought my dry-erase board and drew the little processes and the pipes and everything and couldn't figure out what was going wrong. Thanks again.
  2. In Topic: Converting C++ to C (Phone Mnemonics)

    Posted 5 Nov 2012

    View Postbaavgai, on 05 November 2012 - 02:31 PM, said:

    This is a great example of why C++ programmers have it too damn easy. ;)


    Anyone who doesn't program in C or Assembly has it easy!

    I'm learning both right now and I miss the old days of Java. Anything that involves arrays--and strings by extension--makes me wanna pull my hair out. But it's nice to learn a language you can do so much with.
  3. In Topic: Converting C++ to C (Phone Mnemonics)

    Posted 5 Nov 2012

    View Postjimblumberg, on 05 November 2012 - 07:35 AM, said:

    So using calloc() to allocate memory then using strncpy() is a waste of CPU cycles because you waste time filling the allocated memory with zeros then copy the string, after which you fill the unused space with zeros again.

    Jim


    Wow, that's really good to know. I'll definitely keep that in mind from now on. I'm learning C in my class called "Advanced Programming," after programming in C for about two months I can see where the "Advanced" comes from. C makes me appreciate OOP that much more, but there's something enticing about C :bigsmile:
  4. In Topic: Converting C++ to C (Phone Mnemonics)

    Posted 4 Nov 2012

    Ok, I'm sorry for the ambiguous "proper results," but I was in a bit of a rush when I posted the question.

    Thanks for the insight, I learned to program in Java where + concatenated two strings, and I completely forgot about how strings are handled in C. I always able to fix the problem by using strcat in lieu of '+' and using another method substring that modifies strncpy a bit. For the sake of education, here's the code in case anyone else has a similar problem.

    void permute(char* prefix, char* post) {
       if (strlen(post) == 0)
            printf("%s\n", prefix);
        else {
            char* letters = getChar(*post);
            for (int i = 0; i < strlen(letters); i++) {
    
                //strings needed for recursive premute call
                char *combined = calloc(sizeof(char), 50);
                char *prefix_new = substring(letters, i, 1);
                char *postfix_new = substring(post, 1, strlen(post));
    
                //combines the strings
                strcat(combined, prefix);
                strcat(combined, prefix_new);
                
                permute(combined, postfix_new);
    
                //frees allocated memory
                free(combined);
                free(prefix_new);
                free(postfix_new);
            }
            free(letters);
        }
    }
    
    
    


    Here's the substring method

    char *substring(char *s, int start, int end) {
        char *result = calloc(sizeof(char), 50);
        return strncpy(result, s + start, end);
    }
    
    


    I'm pretty sure it may not be the most elegant solution, but here are the results

    AD
    AE
    AF
    BD
    BE
    BF
    CD
    CE
    CF
    
    


    which are the wanted results, plus no memory leaks--which is always good.

    Thanks for the help

My Information

Member Title:
New D.I.C Head
Age:
Age Unknown
Birthday:
Birthday Unknown
Gender:
Location:
The Countdown City
Years Programming:
1
Programming Languages:
Java, learning C at school, trying to learn Scala on my own

Contact Information

E-mail:
Private

Friends

Xente hasn't added any friends yet.

Comments

Xente has no profile comments yet. Why not say hello?