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

1. ## 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. 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. 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])
{
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. 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. 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. 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?

7. 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. 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>`

9. 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

10. 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.

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.

11. 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