Thread: A "Simple" Array/String Problem. "Help" ASAP needed

  1. #1
    Registered User
    Join Date
    Aug 2015
    Location
    Universiti Teknologi Malaysia
    Posts
    6

    A "Simple" Array/String Problem. "Help" ASAP needed

    well hi!
    i got some interesting coding in mind, but due to not having enough sleep + headache, i cant think clearly, so i need u guys help tho'.

    alright, it's like this
    • i want to make a function of array/string(either one, no hassle)
    • then in that function --> i want another function/variable/anything_u_can_think_of to show me how many letter are there(including space) in the array function
      • it's like this;
      • char A[10]; then lets assume i give A="abcde"
      • so next, there will be a variable/function that can show me that 'A' has 5 letter out of 10 or just 5 is fine


    i just wanna get the basic ideas guys, please help me, i cant think another way out for this one part of my coding. I got tons of another complex coding to work on.

    whoever can solve this problem of mine, is much appreciated

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    i want to make a function of array/string(either one, no hassle)
    I'm assuming you mean a function that receives an array or a string.

    then in that function --> i want another function/variable/anything_u_can_think_of to show me how many letter are there(including space) in the array function
    The approach would depend on whether you were working with a string or just an array.

    If you're only working with an array, you cannot know its size when you pass it to a function, since only a pointer is actually passed.

    Question 6.4

    One common approach is to design the function with an additional size parameter, so you can pass the size of the array along with the array itself.

    If you are passing a string to a function, and only want to know how long the string is (which may be shorter than the size of the array), the function can just go through the array (via the pointer it receives) until it sees a particular value.

    This "particular value" is what differentiates a simple character array from a C-string. If you know the definition of a C-string, you know what character your function should be looking for.

  3. #3
    Registered User
    Join Date
    Aug 2015
    Location
    Universiti Teknologi Malaysia
    Posts
    6

    Question

    alright, to make thing's clearer, this is just a partial of the real coding I working on, its from MPLABX with XC8 compiler, as u can see, its "quite" complicated, but lets just focus on main funtion.

    Code:
    void NextDisplay()
    {
        WriteCmdXLCD(0x01);
        while(BusyXLCD());
    }
    Code:
    void Line1(char Z[15])
    {
        SetDDRamAddr(0x00);
        putrsXLCD(Z);
        delay(100000);
    
        NextDisplay();
    }
    
    
    void main(void)
    {
        init_XLCD();
    
        Line1("ABCDE");
    
    }


    The output of this coding is "ABCDE", but now, i would like to have a certain variable in >>void Line1(char Z[15])<< which it can count how many
    letter are use out of 15.

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    According to the manual for XC8, the function "strlen()" is supported. If you don't know, this stands for string length, which is what you're looking for.

  5. #5
    Registered User
    Join Date
    Aug 2015
    Location
    Universiti Teknologi Malaysia
    Posts
    6
    I know the availability function of "strlen ()", but it's a ready/built-in program, I need a a basic idea on how to make it from scratch, so I can alter everything I want😈, but anyway, thanks for suggesting about C-string, I think I might be already solve this problem, I'll post the solution when it's done and stable in simple C code. But now, I need a sleep😅

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by AirulFmy
    I know the availability function of "strlen ()", but it's a ready/built-in program, I need a a basic idea on how to make it from scratch, so I can alter everything I want
    Why not start by thinking of how to implement strlen, then implement your own version of strlen and test it?
    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

  7. #7
    Registered User
    Join Date
    Aug 2015
    Location
    Universiti Teknologi Malaysia
    Posts
    6

    Thumbs up

    Thanks to you guys both Matticus and Laselight, thanks for the recommendation tho'. i have succesfully understand the basic concept of this <cstring> and strlen(), its quite pretty easy actually

    Code:
    #include <htc.h>
    #include <xlcd.h>
    #include <delays.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include <iostream>
    #include <cstring>
    
    
    int main()
    {    
    
        const char str[] = "abcde";         // which have 5 letter
    
    
        printf("Without NULL = %d \n",strlen(str));    // will show 5 byte is use
    
    
        printf("With NULL = %d",sizeof str);    // will show only 5 byte is use including NULL
    
    
        return 0;
    }
    The Output is
    Code:
    Without NULL = 5
    With Null = 6
    one more thing, this coding just use #include <iostream> and #include <cstring>, the others is just for my use

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Eh, you posted this in the C programming forum and used C constructs, so it is reasonable to suppose that you intended to write a C program, and so you did. However, you included C++ headers:
    Code:
    #include <iostream>
    #include <cstring>
    You should remove the above lines, replacing the inclusion of <cstring> with:
    Code:
    #include <string.h>
    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

  9. #9
    Registered User
    Join Date
    Aug 2015
    Location
    Universiti Teknologi Malaysia
    Posts
    6
    well, for the <iostream> i admitted it is C++, but <cstring> C++? i thought it C (lol),
    but well, because it compile with no problem, thats why i use it, well, does harm will come even if i stick with <cstring>?

    its just a habit, i'm using DEV-C++ software to work on certain concept(like this one) and always tend to include every header i know, LOL
    Last edited by AirulFmy; 08-19-2015 at 04:00 AM.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by AirulFmy
    for the <iostream> i admitted it is C++, but <cstring> C++? i thought it C (lol)
    Yes, <cstring> is the C++ version of the <string.h> header.

    Quote Originally Posted by AirulFmy
    because it compile with no problem, thats why i use it, well, does harm will come even if i stick with <cstring>?
    This just means that you are compiling your C code with a C++ compiler. Some people do this to take advantage of the somewhat stricter type checking of C++, but ultimately C and C++ are different languages, so it is harmful in that if someone tries to compile your code with a C compiler, the compilation will fail.
    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

  11. #11
    Registered User
    Join Date
    Aug 2015
    Location
    Universiti Teknologi Malaysia
    Posts
    6

    Lightbulb

    Some people do this to take advantage of the somewhat stricter type checking of C++
    yes, i'm well aware of this problem, thats why i use Dev-C++, it use both language, but I just use it for checking the concept.
    In real daily life software like MPLABX, i need to choose my own compiler, so if I "accidentally" enter invalid command, i can spotted it right away

    , so it is harmful in that if someone tries to compile your code with a C compiler, the compilation will fail
    well to be honest, i never thought about this, hahaha
    well then, thanks for the remainder, i'll keep in mind to make sure the coding "just" in 1 language next time

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 12-08-2014, 08:12 PM
  2. Replies: 4
    Last Post: 07-17-2012, 09:02 AM
  3. "itoa"-"_itoa" , "inp"-"_inp", Why some functions have "
    By L.O.K. in forum Windows Programming
    Replies: 5
    Last Post: 12-08-2002, 08:25 AM
  4. "CWnd"-"HWnd","CBitmap"-"HBitmap"...., What is mean by "
    By L.O.K. in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 07:59 AM

Tags for this Thread