-1

Why the example code below can run fine on Visual Studio. In Eclipse, NetBean or CodeBlock the code can run but can't show the Result? Thanks All. Ex: input one string. a/ Uppercase first letter. b/ Remove spaces inside the string.

#include "iostream"
    #include "string.h"
    using namespace std;

    #define MAX 255

    //uppercase first letter
    char* Upper(char* input)
    {
        char* output = new char[MAX];

        bool isSpace = false;
        for (int i = 0; i < strlen(input); i++)
        {
            output[i] = input[i];
            if (isSpace)
            {
                output[i] = toupper(output[i]);
                isSpace = false;
            }
            if (output[i] == ' ') isSpace = true;
        }
        output[strlen(input)] = '\0'; // end of the string
        output[0] = toupper(output[0]); // first character to upper

        return output;
    }
    //remove space inside the string
    char* RemoveSpaceInside(char* input)
    {
        char* output = new char[MAX];
        strcpy(output, input);

        int countWhiteSpace = 0;
        for (int i = 0; i < strlen(output); i++)
        {
            if (output[i] == ' ')
            {
                for (int j = i; j < strlen(output) - 1; j++) // move before
                {
                    output[j] = output[j + 1];
                }
                countWhiteSpace++;
            }
        }
        output[strlen(output) - countWhiteSpace] = '\0'; // end of the string

        return output;
    }

    int main()
    {
        char* name = new char[MAX];
        cout << "Enter name: "; cin.getline(name, strlen(name)); 
        cout << "Your name: " << name << endl;

        cout << "\n******* Q.A *******\n";
        char* qa  = Format2VN(name);
        cout <<  qa << endl;

        cout << "\n******* Q.B *******\n";
        char* qb = RemoveSpaceInside(name);
        cout << qb << endl;
        return 0;
    }
LucidLynx
  • 141
  • 8
  • 5
    That's some awful code you got there. I suggest you get your hands on a [good C++ book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) ASAP. – Etienne de Martel Jul 25 '12 at 18:51

1 Answers1

2
char* name = new char[MAX];
cout << "Enter name: ";
cin.getline(name, strlen(name));

Calling strlen(name) will invoke undefined behavior, because you haven't initialized the array. Poor strlen will try to find the NUL character in an uninitialized byte mess. Definitely not a good idea.

What you probably want is:

cin.getline(name, MAX);   // not sure if MAX or MAX-1 or whatever

In general, do yourself a favor and replace char* with std::string. Also, get a good C++ book.

Here is how your example would look like in actual C++:

#include <algorithm>
#include <cctype>
#include <iostream>
#include <string>

std::string upper_first_letter(std::string s)
{
    if (!s.empty()) s[0] = toupper(s[0]);
    return s;
}

std::string remove_spaces(std::string s)
{
    s.erase(std::remove_if(s.begin(), s.end(), isspace), s.end());
    return s;
}

int main()
{
    std::string name;
    std::cout << "Enter name: ";
    std::getline(std::cin, name);

    std::cout << name << '\n';
    std::cout << upper_first_letter(name) << '\n';
    std::cout << remove_spaces(name) << '\n';
}
Community
  • 1
  • 1
fredoverflow
  • 256,549
  • 94
  • 388
  • 662