sábado, 30 de noviembre de 2013

String and Vectors Part 1

An Array Type for String:

C-string...

  • can be used to represent string of characters
  • are stored as arrays of characters
  • use the null character '\0' to end a string
To declare a C-string variable, declare an array of characters: char s[11]
Declaring a C-string as char s[11] creates space for only 10 characters.

A C-string variables does not need a size variable.  The null character immediately follows the last character of the string.

To declare a C-string variable, use the syntax:

char Array_name[Maximum_C_String_Size + 1];

+1 reserves the additional character needed by '\0'


To initialize a C-string during declaration, the null character '\0' is added for you.

Another alternative is:

char short_string[] = "abc";

but not this:

char short_string[] = {'a', 'b', 'c'};

This attempt does not cause the \0 to be inserted in the array.

Very important: If the null character is lost, the array cannot act like a C-string.



Assignment of C-string

A common method to assign a value to a C-string variable is to use strcpy, define in the cstring library.  For example:

#include <cstring>
...
char a_string[11];
strcpy (a_string, "Hello");

Place "Hello" followed by the null character in a_string.

strcpy can create problems if not used carefully
  • strcpy does not check the declared length of the first argument.  
  • It is possible for strcpy to write characters beyond the declared size of the array.
A solution for strcpy is using strncpy
  • strncpy uses a third argument representing the maximum number of characters to copy.
For example:

char another_string[10];
strncpy(another_string, a_string_variable, 9);

This code copies up to 9 characters into another_string, leaving one space for '\0'

The = = operator does not work as expected with C-string.  The predefined function strcmp is used to compare C-string variables.  For example:

#include <cstring>
...
if (strcmp(c_string1, c_string2))
     cout << "Strings are not the same.";
else
     cout << "String are the same.";

strcmp compares the numeric codes of elements in the C-string a character at a time.  If the two C-sting are the same, strcmp returns 0.
As soon as the caracters do not match:
  • strcmp returns a negative value if the numeric code in the first parameter is less
  • strcmp returns a positive value if the numeric code in the second parameter is less
  • Non-zero values are interpreted as true

More C-string Functions:

Another C-string function is strlen.  This function returns the number of characters in a string.

int x = strlen (a_string);

Another one is the strcat function that concatenates two C-strings.
  • The second argument is added to the end of the first
  • The result is placed in the first argument
For example:

char string_var[20] = "The rain";
strcat(string_var, "in Spain");

Now string_var contains "The rainin Spain"

The strncat is a safer version of strcat.  A third parameter specifies a limit for the number of character to concatenate.  For example:

char string_var[20] = "The rain";
strncat(string_var, "in Spain", 11);




C-string as Arguments and Parameters:

C-string variable are arrays.  
C-string arguments and parameters are used just like arrays.
  • If a function change the value of a C-string parameter, it is best to include a parameter for the declared size of the C-string.
  • If a function does not change the value of a C-string parameter, the null character can detect the end of the string and no size argument is needed.
C-string output and input:

C-string can be output with the insertion operator.  For example:

char news[] = 'C-strings";
cout << news << "Wow." << endl;

C-string can be input with the extraction operator.  For example:

char a[80], b[80];
cout << "Enter input: " << endl;
cin >> a >> b;
cout << a << b << "End of Output";

could produce:

Enter input:
Do be do to you!
DobeEnd of Output

Whitespace end reading of data.

C-string input and output work the same way with file streams.  Just replace cin with the name of an input-file stream and the cout with the name of an output-file stream.

Reading an Entire Line:

Predefined member function getline can read an entire line, including spaces.

getline is a member of all input streams that has two arguments, the first is a C-string variable to receive input and the second is an integer, usually the size of the first argument specifying the maximum number of elements in the first argument getline is allowed to fill.

Using getline:

char a[80];
cout << "Enter input: " << endl;
cin.getline(a, 80);
cout << a << "End of Output";

and could produce:

Enter some input:
Do be do to you!
Do be do to you!End of Output

getline stops reading when the number of characters, less one, specified in the second argument have been place in the C-string.

getline syntax:

cin.getline(String_Var, Max_Characters + 1);

cin can be replaced by any input stream
Max_Characters + 1  reserves one element for the null character.

More about C-string...
  • When doing numeric input, it is useful to read input as a string of characters, then covert the string to a number.
    • "1234" is a string characters
    • 1234 is a number
  • To read an integer as characters
    • Read input as characters into a C-string, removing unwanted characters
    • Use the predefined function atoi to convert the C-string to an int value.
      • atoi ("1234") returns the integer 1234
      • atoi("#123") returns 0 because # is not a digit
  • Larger integers can be converted using the predefined function atol
    • atol returns a value of type long
  • C-string can be converted to type double using the predefined function atof
    • atof returns a value of type double
      • atof("9.99") returns 9.99
      • arof("$9.99") returns 0.0 because the $ is not a digit
The conversion functions atoi, atol & atof are found in the cstdlib library.



No hay comentarios.:

Publicar un comentario