C++ Recursion and Pointer Problems

First off, this is for an assignment.

First off, this is for an assignment.

What's required:

1.Take in string input
2. copy string (stripping out white space, punctuation, and
covert all characters to uppercase in the process)
3. Then determine if this copied string is a palindrome.

The method required for determining a palindrome:

Base Case:  string length is <= 1
General Case: if first letter != last letter, false, otherwise
point to next letter and write '\0' to last letter and
call the method again

For example:

RACECAR\0   R==R
ACECA\0     A==A
CEC\0       C==C
E\0         E <= 1  TRUE!

I cannot get my isPalindrome function to work correctly. Everything else is spot on, as far as I can tell. I really think the problem lies in my recursive call. I have been debugging this for 2 days and I cannot figure why the return is wrong. Any help would be GREATLY appreciated. I'm not looking for a hand out, maybe just some extra eyes on this code. Thanks.

#include <iostream>
#include <string>
#include <cctype>
using namespace std;

int charCount(char * copy)
{
int count = 0;

for (int i = 0; copy[i] != '\0'; i++)
{
count++;
}

return count;

}

bool isPalindrome(char *copy)
{
bool result = false;
int size = charCount(copy);

char * last = &amp;copy[size - 1];

if (size &lt;= 1)
{
result = true;
}

if (copy != last)
{
result = false;
}

else
{
++copy;
last = '\0';
isPalindrome(copy);
}

return result;

}

void stringCopy(char * source, char * destination)
{
int sourceIndex = 0;
int destIndex = 0;

while (source[sourceIndex] != '\0')
{
while (!(isalnum(source[sourceIndex])) &amp;&amp; source[sourceIndex] != '\0')
{
sourceIndex++;
}

if (source[sourceIndex] == '\0')
{
break;
}

if (isalpha(source[sourceIndex]))
{
destination[destIndex] = toupper(source[sourceIndex]);
}

if (isdigit(source[sourceIndex]))
{
destination[destIndex] = source[sourceIndex];
}

sourceIndex++;
destIndex++;
}

destination[destIndex] = '\0';

}

int main()
{
string input = "";

cout &lt;&lt; "Enter a string: ";
getline(cin, input);

char * source = &amp;input[0];
int sourceSize = charCount(source);

char * copy = new char[sourceSize];

stringCopy(source, copy);

int copySize = charCount(copy);

if (isPalindrome(copy))
{
cout &lt;&lt; input &lt;&lt; " is a palindrome!" &lt;&lt; endl;
}

else
{
cout &lt;&lt; input &lt;&lt; " is not a palindrome" &lt;&lt; endl;
}

return 0;

}

C/C++ vs. Rust: A developer’s perspective

In this post, you'll see the difference between Rust and C/C++ in a developer’s perspective

C++ is an incredibly fast and efficient programming language. Its versatility knows no bounds and its maturity ensures support and reliability are second to none. Code developed in C++ is also extremely portable, all major operating systems support it. Many developers begin their coding journey with the language, and this is no coincidence. Being object-oriented means it does a very good job of teaching concepts like classes, inheritance, abstraction, encapsulation and polymorphism. Its concepts and syntax can be found in modern languages like C#, Java and Rust. It provides a great foundation that serves as a high speed on ramp to the more popular, easier to use and modern alternatives.

Now it’s not all rosy. C++ has a very steep learning curve and requires developers to apply best practices to the letter or risk ending up with unsafe and/or poor performing code. The small footprint of the standard library, while most times considered a benefit, also adds to the level of difficulty. This means successfully using C++ to create useful complex libraries and applications can be challenging. There is also very little offered in terms of memory management, developers must do this themselves. Novice programmers could end up with debugging nightmares as their lack of experience leads to memory corruption and other sticky situations. This last point has lead many companies to explore fast performing, safe and equally powerful alternatives to C++. For today’s Microsoft that means Rust.

The majority of vulnerabilities fixed and with a CVE [Common Vulnerabilities and Exposures] assigned are caused by developers inadvertently inserting memory corruption bugs into their C and C++ code - Gavin Thomas, Microsoft Security Response Center
Rust began as a personal project by a Mozilla employee named Graydon Hoare sometime in 2006. This ambitious project was in pre-release development for almost a decade, finally launching version 1.0 in May 2015. In what seems to be the blink of an eye it has stolen the hearts of hordes of developers going as far as being voted the most loved language four years straight since 2016 in the Stack Overflow Developer Survey.

The hard work has definitely paid off. The end result is very efficient language which is characteristically object oriented. The fact that it was designed to be syntactically similar to C++ makes it very easy to approach. But unlike the aforementioned it was also designed to be memory safe while also employing a form of memory management without the explicit use of garbage collection.

The ugly truth is software development is very much a trial and error endeavor. With that said Rust has gone above and beyond to help us debug our code. The compiler produces extremely intuitive and user friendly error messages along with great direct linking to relevant documentation to aid with troubleshooting. This means if the problem is not evident, most times the answer is a click away. I’ve found myself rarely having to fire up my browser to look for solutions outside of what the Rust compiler offers in terms of explanation and documentation.

Rust does not have a garbage collector but most times still allocates and release memory for you. It’s also designed to be memory safe, unlike C++ which very easily lets you get into trouble with dangling pointers and data races. In contrast Rust employs concepts which help you prevent and avoid such issues.

There are many other factors which have steered me away from C++ and onto Rust. But to be honest it has nothing to do with all the great stuff we’ve just explored. I came to Rust on a journey that began with WebAssembly. What started with me looking for a more efficient alternative to JavaScript for the web turned into figuring out just how powerful Rust turns out to be. From its seamless interop…

Automatically generate binding code between Rust, WebAssembly, and JavaScript APIs. Take advantage of libraries like web-sys that provide pre-packaged bindings for the entire web platform. – Rust website
To how fast and predictable its performance is. Everything in our lives evolves. Our smartphones, our cars, our home appliances, our own bodies. C++ while still incredibly powerful, fast and versatile can only take us so far. There is no harm in exploring alternatives, especially one as exceptional and with as much promise as Rust.

What do you guys think? Have you or would you give Rust a try? Let us know your thoughts in the comments section below.

If you liked this post, share it with all of your programming buddies!