Allrite so I'm writing this program - and I've been having some trouble with it - there are three parts to it

1. Write a function

void tolower(char* s)

that replaces all uppercase characters in the C-style string s with lower case letter.

- Two helper functions to use
a)

bool isupper(char c)

which returns true is c is an uppercase letter and false if not.
b)

char tolower(char c)

whicch returns lowercase letters

**** I know there are standard library functions that DO this - the point is not to use ANY of them lol

2.Write a function

char* cat_dot(const char *s1, const char* s2, const char* sep)

that returns C string of s1 followed by sep followed by s2 - the default of sep is "."

SO is should look something like this:
cat_dot("hello", "world") outputs "hello.world"
cat_dot("hello", "world", "&*^") outputs "hello&*^world"

3.Write a new version of is_palindrome that makes a backward copy of the string and then compares to the original and says "Not a palindrome"

So yeah...ugh -.-
Pretty much I have all the skeletal parts of the program - whichc meats all hte important parts aren't even in -.-

Please post what you have thusfar and indicate some specific areas in which you require the most help.

#include <iostream>
using namespace std;

const char* get_c_string();
int strlen(const char* s);
void tolower(char* s);
char* strdup (const shar* s);
char* cat_dot(const char *s1, const char* s2, const char* sep=".");
bool is_plaindrome(const char* s);

//test out our functions

int main()
{
    cout <<"tolower/strdup test:\n";
    cout <<"Enter a mixed-case string: ";
    const char* s = get_c_string();
    cout <<"The original string is \"" << s << "\"n";
    char *s1 = strdup(s);
    tolower(s1);
    cout << "Translated to lower case:\"" << s1 << "\"n";

    cout << "\ncat_dot test\n";
    char str1[] = "bonjour";
    char str2[] = "olam";
    // use default seperator
    cout << "cat_dot(\"" << str1 << "," << str2 << "\")is\""
	 << newsep << "\")is\""
	 << cat_dot(str1, str2) << "\"\n";
    // use new seperator
    char newsep[] = "#!?";
    cout << "cat_dot(\"" << str1 << "\"," << str2 << "\","
	 << newsep << "\") is\""
	 << cat_dot(str1, str2, newsep) << "\"\n";

    cout << "\nPalindrome test:\n";
    cout << "Enter palindrome candidates (one per line).\n"
	"End-of-file (control-D) terminate input (and program).\n";
    while (true) {
	const char* pal_cand = get_c_string();
	if (!cin)
	    break;
	cout << "\"" << pal_cand << "\" is ";
	if (!is_palindrome(pal_cand)) cout << "not";
	cout << "a palindrome.\n";
    }
}

//returns the length of a c-string
int strlen(const char *s)
{
    int len = 0;
    while (s[len] != 0)
	len++;
    return len;
}

//foolproof way to get a C-string from input
//not exactly in the spirit of C
//suffers from a memory leak

const char* get_c_string()
{
    string str;
    getline(cin, str);
    const char* s  = str.c_str(); //memory leak!
    return s;
}

bool isupper(char c)
{
[B]//blank[/B]
    return true;
}

char tolower(char c)
{
[B]//blank[/B]
    return c;
}

void tolower(char* s)
{
[B]//blank[/B]
}

char* strdup(const char* s)
{
[B]//blank[/B]
    return 0;
}

char* cat_dot(const char *s1, const char* s2, const char* sep)
{
[B]//blank[/B]
    return 0;
}

char* reverse(const char* s)
{
[B]//blank[/B]
    return 0;
}

bool equal(const char* s1, const char* s2)
{
[B]//blank[/B]
    return true;
}

bool is_palindrome(const char* s)
{
[B]//blank[/B]
    return true;
}

BOLD are the areas I need help on

For the others one of the central themes is knowing where to allocate your space for the string and where to delete it.

What is it about the nature of "s" here that creates a problem?

const char* s  = str.c_str(); //memory leak!

Think about the variable's lifetime.

Edited 5 Years Ago by jonsca: n/a

This article has been dead for over six months. Start a new discussion instead.