Thread: Need help with strings, pointers and custom functions

  1. #1
    Registered User
    Join Date
    May 2017
    Posts
    101

    Need help with strings, pointers and custom functions

    Hi,

    I have some excercises about strings to concadenate, copy strings and sorting using pointers and functions but I am not able to start. Let me to explain, I barely know how to do that in int main () but not using custom functions, I am completely lost at this point. Is there any book or tutorial that explain this? I have been looking for tutorials but most of them solve those problems in int main(). I think a book may be a better solution.

    Thank you in advance.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Do you know how to define your own functions (e.g., to print "hello world!", and then to print an argument, etc), how to forward declare them, and how to call them?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2017
    Posts
    101
    Quote Originally Posted by laserlight View Post
    Do you know how to define your own functions (e.g., to print "hello world!", and then to print an argument, etc), how to forward declare them, and how to call them?
    Hi laser, you again, ready to destroy me, LOL. Up to know I wrote all the codes within int main (). I used printf, scanf, gets, puts and as far as I remember nothing else. My knowledge is limited by those concepts + conditionals + loops + nested loops + arrays + pointers + strings, everything within int main(). Because I can barely explain (moreover I am not an english speaker), let me to give you an example of what I need to learn as quickly as possible:

    As an example, a code to count vowels in a string, the last thing we did at class but my teacher wen too fast and explained in less than 5 minutes because the class was almost over:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int contar_caracter(char *string, char ch) {
        int counter=0;
        char *str=string;
        while (*str!='\0') {
            if (*str==ch) {
                counter++;
            }
            str++;
        }
        return counter;
    }
    
    
    int main ()
    {
        char text[1024];
        char *p_text=text;
    
    
        printf("Insert text: ");
        gets(p_text);
    
    
        int cont_a=cont_character(p_text,'a') + cont_character(p_text,'A');
        int cont_e=cont_character(p_text,'e') + cont_character(p_text,'E');
        int cont_i=cont_character(p_text,'i') + cont_character(p_text,'I');
        int cont_o=cont_character(p_text,'o') + cont_character(p_text,'O');
        int cont_u=cont_character(p_text,'u') + cont_character(p_text,'U');
    
    
        printf("%d a, %d e, %d i, %d o, %d u \n",cont_a++,cont_e++,cont_i++,cont_o++,cont_u++);
        return 0;
    }
    I have some excercises related to this but I don't want to bother you if even I don't understand this code. So, this is the reason I need some resources to understand anything...

    Laser, I really appreciate your patience.

    Thank you.

  4. #4
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Location
    CA, USA
    Posts
    466
    Code:
    printf("%d a, %d e, %d i, %d o, %d u \n",cont_a++,cont_e++,cont_i++,cont_o++,cont_u++);
    return 0;
    No need to "post-increment" the variables, as you return immediately, and the incremented values are never used?

    Is that you're verbatim professor's code?

  5. #5
    Registered User
    Join Date
    May 2017
    Posts
    101
    This excercise was solved by my teacher, is a course, I am not in university. I literally copied & pasted the code but I forgot to translate the first line that is still in spanish, sorry. I need to understand this kind of things to proceed with excercises.

  6. #6
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Location
    CA, USA
    Posts
    466
    Basically, those 2 lines of code:

    Code:
    printf("%d a, %d e, %d i, %d o, %d u \n",cont_a++,cont_e++,cont_i++,cont_o++,cont_u++);
    return 0;
    is equivalent to

    Code:
    printf("%d a, %d e, %d i, %d o, %d u \n",cont_a,cont_e,cont_i,cont_o,cont_u);
    return 0;
    but it is NOT true that

    Code:
    printf("%d a, %d e, %d i, %d o, %d u \n",cont_a++,cont_e++,cont_i++,cont_o++,cont_u++);
    is equivalent to

    Code:
    printf("%d a, %d e, %d i, %d o, %d u \n",cont_a,cont_e,cont_i,cont_o,cont_u);
    in general.

  7. #7
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Location
    CA, USA
    Posts
    466
    Ok, nevermind about that detail. It was too detailed.


    Whats the confusion here? You understand that the function

    Code:
    int contar_caracter(char *string, char ch)
    counts the number (an integer) of characters which are equal to the character (parameter ch) in the sequence of characters (parameter string) ?

    Then the code is straighforward: Read a line, count the each of the vowels (A,E,I,O,U) in it.

    Then there is a bit of nonsense to allow for upper or lower-case vowels.

    Then it prints each of how many of each vowel was in the input string.

    EDIT: I'm considering to post this "professor's" code to the Daily WTF...

  8. #8
    Registered User
    Join Date
    May 2017
    Posts
    101
    Quote Originally Posted by MacNilly View Post
    Ok, nevermind about that detail. It was too detailed.


    Whats the confusion here? You understand that the function

    Code:
    int contar_caracter(char *string, char ch)
    counts the number (an integer) of characters which are equal to the character (parameter ch) in the sequence of characters (parameter string) ?

    Then the code is straighforward: Read a line, count the each of the vowels (A,E,I,O,U) in it.

    Then there is a bit of nonsense to allow for upper or lower-case vowels.

    Then it prints each of how many of each vowel was in the input string.

    EDIT: I'm considering to post this "professor's" code to the Daily WTF...
    I can understand what this code does but I need to understand those "functions" that you write before int main (), sorry, I don't know how to explain. Because up to know, everything I've done is writing a code between int main () and return 0. Now we are with "custom functions" or whatever the term in english, translation from spanish would be more or less: "functions for strings" this is the heading of the excercises, LOL. This excercise was "vomited" in a matter of 2 minutes, so I need to understand the basics of making my own functions and the thing where I am completely lost how this function is connected with int main (). I don't even know what to put in the custom function and int main () so I am not able to solve anything. That's the reason I need a tutorial / a book / or even a coah, LOL.

  9. #9
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Location
    CA, USA
    Posts
    466
    For some reason I can't post the link. Just google "The C programming Language by Brian W. Kernighan and Dennis M. Ritchie." It's a free PDF and covers everything C, including functions.

    BTW, C doesn't really have "strings". It has "char" and "pointers" to char (or "array" of chars).
    Last edited by MacNilly; 05-13-2017 at 07:54 AM.

  10. #10
    Registered User
    Join Date
    May 2017
    Posts
    101
    Quote Originally Posted by MacNilly View Post
    For some reason I can't post the link. Just google "The C programming Language by Brian W. Kernighan and Dennis M. Ritchie." It's a free PDF and covers everything C, including functions.

    BTW, C doesn't really have "strings". It has "char" and "pointers" to char (or "array" of chars).
    Thank you MacNilly. I skipped some excercises about functions with chars because I found them too complex and solved others we have as homework with functions but easier with some numerical problems. Soon I will come back to ask when I get stuck with chars and functions. I still find it tedious.

  11. #11
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Location
    CA, USA
    Posts
    466
    No worries, I know (knew) some "professional" (paid) programmers who stated that they really don't "get" pointers, or at least C pointers. Every language has "pointers" whether they are hidden behind an abstraction. But for programming in C, there is no abstraction, and it is vital to understand pointers in order to program seriously in C.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. printing out strings with functions and pointers :(
    By kiwi101 in forum C Programming
    Replies: 7
    Last Post: 10-23-2012, 07:59 AM
  2. Passing pointers to strings to functions
    By drclark in forum C Programming
    Replies: 5
    Last Post: 08-15-2011, 08:05 PM
  3. advise me on custom functions
    By CoCo in forum C Programming
    Replies: 2
    Last Post: 01-21-2011, 08:42 AM
  4. [VERY Beginner question] Strings + functions + ...pointers?!
    By arthurhess in forum C++ Programming
    Replies: 4
    Last Post: 12-08-2009, 02:44 PM
  5. Custom gets and puts functions
    By Silverdream in forum C Programming
    Replies: 2
    Last Post: 02-18-2002, 03:12 PM

Tags for this Thread