pointers and char functions without c++ library

Pages: 12
Apr 10, 2020 at 12:47am
I'm a little stuck I'm trying to print an array backwards among other things. Here is what I have:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/***************************************************************************************/
//Function displays what the user is prompt to enter.
void printOriginal(char words[])
{
    cout<<endl;
    cout<<"Original Order"<<endl;

    char* p; //pointer for the array.
    p = words;

    while(*p != '\0')//checks the for last character
    {
        cout<<*p;
        p++;
    }
        system("pause");
}

/***************************************************************************************************************/
void printReverse(char words[])//Function displays text in reverse.
{

    cout<<endl;
    cout<<"\nReverse Order"<<endl;

    char *p;
    p = words;

    while(*p = '\0')
          {
             p--;
             cout<<*p;

          }

    system("pause");
}
/**********************************************************************************************************************/
Apr 10, 2020 at 1:16am
printOriginal looks pretty good, but you could shorten it up:
1
2
3
4
void printOriginal(char /*const*/ s[])
{
  while (*s) std::cout << *s++ << '\n';
} 


To print a string backwards, you can use the following straightforward algorithm.
1. Let len be the length of the string s.
2. If len is zero (the string is empty), terminate.
3. Otherwise, Let i = len - 1 be the index of the last character in the string.
4. Print the i'th character of the string s[i]
5. Let i = i - 1.
6. If i is nonnegative (i is a valid index into the string s), go to step 4.
7. Otherwise, terminate.

Work on step 1.

Can you write the function
int myStrlen(char s[])
That counts the number of characters in a string?

The terminating NUL character only exists to signal the end of the string, so don't include it in the count.
Last edited on Apr 10, 2020 at 1:17am
Apr 10, 2020 at 2:06am
Alternatively to writing your own strlen you could just run a pointer to the end and then run it backwards until it equals s again.

1
2
3
const char *p = s;
while (*p) ++p;
while (p-- > s) std::cout << *p;

Apr 10, 2020 at 3:14am
@dutch, do you mean for the reverse function?
Apr 10, 2020 at 12:59pm
@OP,

Yes, he does mean for the reverse function.

However, more importantly, do you understand what @dutch's code does? It's only 3 lines long. Can you explain what each line does? If so, then you can figure out what this code snipped is doing.

If you don't understand this code (and pointer manipulation is confusing to beginners--we've all been there), ask a question.
Apr 10, 2020 at 2:01pm
How does one print using the pointer like this: olleh
Apr 10, 2020 at 2:16pm
I kinda do, but I get to print in reverse you need to go to the last character and backwards. So, thats what the while loop is for.
Apr 10, 2020 at 7:14pm
are you allowed recursion? It naturally reverses things with less aggravation.

void printrev(char* cp)
{
if(*cp)
{
printrev(cp+1);
cout << *cp;
}
}


Last edited on Apr 10, 2020 at 7:15pm
Apr 10, 2020 at 7:22pm
@jonnin, Why in the world would you not use code tags! ;)
Apr 10, 2020 at 8:12pm
only restriction I think is I can't use C++ library.
Apr 10, 2020 at 8:49pm
If you can't use C++ libraries, then using <iostream> is probably verboten.

Writing function parameters lists when passing arrays you really should pass the size of the array(s) as well. You won't have a null ('\0') terminator when the array is not a C string.
Apr 10, 2020 at 9:12pm
Well dang. I don't know how. Using c library would make it so easy lol. I guess that's the point. I honestly never seen anything printed like that in my book: olleh
Apr 10, 2020 at 9:43pm
https://www.tutorialspoint.com/cplusplus/cpp_passing_arrays_to_functions.htm

Passing the size of an array into a function isn't all that hard. By passing the size you can pass arrays that are not the same size.

double getAverage(int arr[], int size);

When you get to learning about templates having a function with an array and its size becomes much easier to deal with.

1
2
template <typename T, size_t N>
void func(T (&x)[N]);


When writing new code I try to avoid regular arrays and C strings as much as possible. C++ containers such as std::vector and std::string.
Apr 10, 2020 at 10:00pm
So in this case if I wanted to print backwards (olleh) I would need to know the size. I can't use size_t I don't think. The make size of the array is 256 but that doesn't mean that will all be used up.
Apr 10, 2020 at 10:02pm
Could I use the '\0' in some way?
Apr 10, 2020 at 10:03pm
Look at cppreference's strlen C++ page.
https://en.cppreference.com/w/cpp/string/byte/strlen

It has code for a possible implementation, using char pointers to find the null terminator ('\0').

1
2
3
4
5
6
std::size_t strlen(const char* start)
{
   const char* end = start;
   while (*end++ != 0);
   return end - start - 1;
}

If you are wondering what the size_t data type is:
http://www.cplusplus.com/reference/cstring/size_t/
Apr 10, 2020 at 10:14pm
oh I thought strlen was a no go because its c++ library. To clarify, c++ library string functions I can't use.
Last edited on Apr 10, 2020 at 10:16pm
Apr 10, 2020 at 10:24pm
strlen was part of the C library <string.h> before there was C++.

cppreference's C++ Standard Library headers page (down towards the bottom of the page) points out the differences of the two different style of headers.
https://en.cppreference.com/w/cpp/header
Apr 10, 2020 at 10:33pm
If you can't use the library provided strlen function, then use cpprereference's "possible implementation" to write your own function. It uses char pointers to find '\0' to get the C string's length.

It will be our little secret, you don't need to let your instructor know.
Apr 10, 2020 at 10:55pm
Should I do something like this:

1
2
3
4
5
6
7
8
  char* p;

   length = 0;
   while(*p[length])
   {
       ++length;
       return length; 
   }


to find the length or am I way off? Then use length. I have no clue how to use that in a loop though.
Pages: 12