UNIDAD 3: ARREGLOS
TEMA LINUX GCC 4: STRINGS
Recordar que en CPP existen dos casos diferentes de strings y cada una de ellas es apropiada pata una serie de TAREAS PROGRAMACION LINUX GCC diferentes, los casos son:
STRING COMO ARREGLO DE CARACTERES
Las strings no son mas que un caso especial de arreglo de caracteres y como tales presentan ciertas peculiaridades que no son posible resolver con metodos tradicionales.
Por ejemplo no es valido usar simples sentencias de asignación o comparación.
1.- char nombre [30],copia[nombre];
copia = nombre; esto no es valido en CPP
2.- if(opcion==`si`)
Tampoco es valido en CPP
Para casos como estos se deberan usar ciertas funciones de manipulación de strings que provee el lenguaje C, estas funciones son :
#include <string.h>
a) STRCPY(string destino,string original);
Se utiliza para copiar el contenido de un string original en un string de destino.
ejemplo:
char nombre[30],copia[30];
nombre= getstring(“nombre”);
strcpy(copia,nombre);
strcpy(nombre,”pato”);
b) STRCAT(string1,string2);
Encadena o agrega al final de la string1 la string2.
ejemplo: char nombre[30],copia[30];
strcpy(nombre,”pepe”);
strcat(nombre,”lopez”);
cout « nombre ;
c) STRCMP(string1,string2);
Se utiliza para comparar 2 strings, esta función regresa cero si ambas strings son iguales, regresa un numero mayor que cero si string 1 es mayor que string 2 y regresa un numero menor que cero si string 1 es menor alfabeticamente que string 2.
ejemplo: char opcion[3];
strcmp(opcion,”si”);
if(strcmp(opcion,”si”)==0)
d) STRLEN(string);
Esta función regresa la cantidad de caracteres que tiene la string.
ejemplo: char nombre[30];
strcpy(nombre,”juan”);
cout « sizeof(nombre);—→REGRESA 30
cout « strlen(nombre);—→REGRESA 4
STRING COMO CLASE
| Function/Operation | Description |
| Var = string2 Var.assign(”string-to-assign) | Assignment of value to string |
| Var.swap(string2) swap(string1,string2) | Swap with value held in string2. Function swap will exchange contents of two string class variables. |
| Var += string2 Var.append() Var.push_back() | Append string/characters. |
| Var.insert() | Insert characters |
| Var.erase() Var = ”” | Clear string variable. No arguments necessary. |
| + | Concatenate |
| ==, !=, <, ⇐, >, >= Var.compare(string) | Compare strings. |
| Var.length() | Return length of string. No arguments necessary. |
| Var.size() | Return length of string. No arguments necessary. |
| Var.capacity() | Return length of string + 1 (NULL??). Number of characters that can be held without re-allocation. No arguments necessary. |
| Var.max_size() | Returns a very large number. No arguments necessary. |
| Var.empty() | Returns 1 if an empty string. Returns 0 if not empty. |
| « | Output stream |
| » getline() | Input stream |
| Var.c_str() | Returns C string pointer. C char string is null terminated. Do not free memory using this pointer! |
| Var.data() | Returns C string pointer. C char string is NOT null terminated. Do not free memory using this pointer! |
| Var[] Var.at(integer) | Access individual characters. |
| Var.find() Var.rfind() | Find first occurance of string or substring. Find last occurance of string or substring. |
| Var.find_first_of() Var.find_last_of() | Find strings and substrings. |
| Var.find_first_not_of() Var.find_last_not_of() | Find strings and substrings. |
| Var.replace() | Replace section of string with new characters. |
| Var.substr() | Return substring of text |
| Var.begin() Var.end() | Itterators |
| Var.rbegin() Var.rend() | Reverse iterators |
Fuente: http://www.yolinux.com/TUTORIALS/LinuxTutorialC++StringClass.html
Primero entender que declarar una variable de tipo string ( ej, string nombre;) es una manera corta de crear un objeto de esta clase.
Segundo las variables nombre, ciudad, etc, no son variables realmente son objetos derivados de esta clase y por tanto puede usar todos los metodos de la tabla de rriba, por ejemplo nombre.length(), ciudad.erase(), etc.
CONSTRUCTORES Y DESTRUCTORES
Constructors:
string sVar1("abc");
string sVar1(//C-string//);
string sVar2(10," "); // Generate string initialized to 10 blanks.
string sVar3(Var1,//string-index//); // Initialize with characters from string starting with index //string-index//.
string sVar4(//iterator-index-begin, iterator-index-end//)
Destructor:
Var.~string(); // Destructor
Replace:
FUENTE: http://www.yolinux.com/TUTORIALS/LinuxTutorialC++StringClass.html
Como se observa String es un clase con muchos metodos muy utiles para el tratamiento de strings, metodos que deberan ser estudiados cuidadosamente con la ayuda del CPP.