8
$\begingroup$

Why do you think it is that most C++ instructors teaching college level computer sciences discourage or even forbid using strings for text, instead requiring students to use character arrays?

I am assuming this methodology is somehow intended to teach good programming habits, but in my experience I don't see anything wrong with just using strings, and they are significantly easier to use and learn.

$\endgroup$
6
  • 9
    $\begingroup$ Do you have a reference for the claim that " most C++ instructors teaching college level computer sciences discourage or even forbid using strings for text, instead requiring students to use character arrays?" I'm not sure that's true, in general. If it is, I suspect it's for the same reason that professors forbid or discourage the use of any libraries: because students learn more about fundamentals by doing things, themselves, from scratch? $\endgroup$
    – Patrick87
    Apr 20, 2012 at 2:21
  • $\begingroup$ I know the professors at Portland State University do, and from what I have read on some forums about using character arrays vs strings that seemed to be generally true. I could be totally wrong, that's just what I have garnered in my (limited) experience. $\endgroup$ Apr 20, 2012 at 3:26
  • 3
    $\begingroup$ @TaylorHuston, you should probably ask those professors. $\endgroup$
    – svick
    Apr 20, 2012 at 6:38
  • 4
    $\begingroup$ Welcome! This is a programming (not quite CS) didactics question, and it asks for opinion. Voted to close thusly. By the way, at my university they don't teach C++ at all. Professors do have a tendency to avoid best practice in their code, though. I guess that is because they are computer scientists, not programmers. $\endgroup$
    – Raphael
    Apr 20, 2012 at 7:59
  • 4
    $\begingroup$ @Raphael Inasmuch as these courses are often taught by computer scientists and are often an introduction to programming and computer science at the same time, I think the question is on-topic here, and this is a proper place for it since the audience (the potential answerers) would be computer scientists. I do doubt the claim, but questions with wrong assumptions are ok and should be answered with a refutation. $\endgroup$ Apr 20, 2012 at 22:47

6 Answers 6

7
$\begingroup$

For a CS student, the professor may discourage std::string and ask for char array, so as to teach basics of array. In my school, the teacher asked us to write C routines like strlen, strcmp etc. on our own. It can't get any low level then this (leaving assembly!).

By using array, instead of string, you can learn memory-management, string operations, notably parsing string. By iterating over array, you are basically parsing string, looking for white-space, special chars, tokens etc.

But I am not recommending array over string, at all. I am just giving a reason to cope with arrays, in university.

$\endgroup$
7
$\begingroup$

Koenig and Moe's Accelerated C++ and Stroustrup's Programming: Principles and Practice Using C++ are using std::string and other components from the standard library from the start. If I remember correctly, they don't even present C arrays for Koenig and Moe and Stroustrup does it quite late in the book (perhaps even just in an annex).

That approach, qualified as modern, is the preferred one in my circles. The other approach, often qualified as historic, follows more closely how C++ evolved, but doesn't take advantage of "newer" (relatively, they are 15 years old now) development and doesn't teach what is practised now (at least on projects not encumbered with too much legacy) but 10 years ago (there is always a lag before being mainstream).

$\endgroup$
4
$\begingroup$

I can't say anything about "most" versus "some" programming instructors, but I can say that in a CS curriculum, the early programming courses have two goals:

  • Prepare students for software engineering-type topics
  • Get students used to what the basic operations a computer does are and how different implementations of higher-level concepts like strings behave

These are somewhat at odds sometimes, and different teachers will make different tradeoffs.

$\endgroup$
2
  • 2
    $\begingroup$ You forget a least a third goal: teach prerequisites for other courses and mandatory internships. $\endgroup$ Apr 20, 2012 at 11:31
  • 1
    $\begingroup$ If the course is algorithms, the low-level approach is better. If the course is information retrieval, the high-level approach is better. Different courses want different things. $\endgroup$
    – Louis
    Apr 20, 2012 at 12:10
4
$\begingroup$

I guess it depends on what you're trying to teach in that course... "Computer Science" is a somewhat vague definition.

If the course is on Algorithms and Data Structures, then teaching students about std::string has absolutely no didactic value. Using char arrays as an introduction to arrays and operations thereon, however, is quite legitimate.

If the course is on object design, then I guess std::string has some value as an example of an abstract data type.

If the course is just on best practices in C++, then I'd worry about the kind of college your at and how they design their Computer Science curriculum.

$\endgroup$
2
$\begingroup$

I guess you misunderstood the actual goal of your course, as good C++ (basically) uses the abstractions provided by the standard library (why would you want to reinvent the wheel?).

There is even a quote by Stroustrup saying "keep arrays out of interfaces" in his FAQ where he gives some hints how to write safe code. Therefore I doubt that you are actually told to not use them, because they are bad, but to learn some fundamentals of how things work, so you can later use that understanding if you actually need to get into optimization or other work, for which you need the real basics.

$\endgroup$
0
$\begingroup$

In some case string are good in other they arent same for char

/**Exemple 1 string use in this case string is cut by Spaces at the output if we wrote del test.txt the the output will be Line 1 del Line 2 test.txt wich is not good **/

#include <iostream>
#include <stdlib.h>
using namespace std ; 

string get_input ;

int main()
{
main:
    cin >> get_input ;
    system(get_input.c_str()) ;
    goto main ;
}

/** Exemple 2 same program but we pass the input in a char with a field of 80 the output for the same cmd as first exemple will be del test.txt on the same line wich good**/

#include <iostream>
#include <stdlib.h>
using namespace std ; 

char input[80] ;
string get_input ;

int main()
{
main:
    cin.getline(input ,80) ;
    get_input = input ;
    system(get_input.c_str()) ;
    goto main ;
}

Strings are objects that represent sequences of characters.

The standard string class provides support for such objects with an interface similar to that of a standard container of bytes, but adding features specifically designed to operate with strings of single-byte characters.

The string class is an instantiation of the basic_string class template that uses char (i.e., bytes) as its character type, with its default char_traits and allocator types (see basic_string for more info on the template).

Note that this class handles bytes independently of the encoding used: If used to handle sequences of multi-byte or variable-length characters (such as UTF-8), all members of this class (such as length or size), as well as its iterators, will still operate in terms of bytes (not actual encoded characters). -C++ Reference http://www.cplusplus.com/reference/string/string/ [1]:

i suggest to look at:

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.