14caracteresycadenas-1231097358820148-1

Upload: deneb-asecas

Post on 07-Apr-2018

215 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/6/2019 14caracteresycadenas-1231097358820148-1

    1/15

    Caracteres y Cadenas

  • 8/6/2019 14caracteresycadenas-1231097358820148-1

    2/15

    Conceptos Bsicos

    Caracteres Valor entero representado como caracter entre

    comillas simples. Por ejemplo: 'z' representa al valorentero de z

    Internamente se representa como un tipo de datoenumerado usando el cdigo ASCII (cdigo estndaramericano para el intercambio de informacin).

    Cadenas Es un arreglo de caracteres que:

    Puede incluir letras, dgitos y caracteres especiales (*, /, $) Tiene un puntero al primer caracter Cuyo valor de la cadena es la direccin de memoria del

    primer elemento.

  • 8/6/2019 14caracteresycadenas-1231097358820148-1

    3/15

    1. Los cdigos para los caracteres querepresentan dgitos del 0 al 9 sonconsecutivos.

    2. Las letras en el alfabeto estn divididosen dos rangos: uno para las maysculas(A-Z) y otro para las minsculas (a-z).

    Sin embargo dentro de cada rango losvalores ASCII son consecutivos.

    Propiedades Importantes delCdigo ASCII

  • 8/6/2019 14caracteresycadenas-1231097358820148-1

    4/15

    Constantes de Tipo Caracter Es un estndar para referirse a un carcter

    especfico en C.

    Para referirse al cdigo ASCII de la letra A,

    se especifica A, el cual es el 65. Para referirse al cdigo del carcter 9, de

    forma similar, 9.

    CUIDADO: El referirse al carcter, no es lo mismo que referirse al valorentero. El 9 es diferente del 9.

  • 8/6/2019 14caracteresycadenas-1231097358820148-1

    5/15

    Operaciones con Caracteres

    Se puede:

    Sumar un entero a un carcter

    Restar un entero de un caracter Restar un caracter de otro

    Comparar dos caracteres entre s

    CUIDADO: Al sumar o restar el resultado no debe salirse del rango

    de representacin ASCII

  • 8/6/2019 14caracteresycadenas-1231097358820148-1

    6/15

    Manejo de Cadenas

    Definicin Como un arreglo de caracteres o una variable de tipo char *

    char color[] = "blue";

    char *colorPtr = "blue";

    Recuerde que una cadena se representa como un arreglo de

    caracteres y termina con '\0' color tiene 5 elementos

    Lectura Utilizando scanf

    scanf("%s", cadena);

    Copia la entrada en el arreglo cadena[] No se necesita el & (porque una cadena es un puntero)

    Recuerde dejar espacio en el arreglo para el fin de cadena'\0

    Escritura Utilizando printf

    printf(%s,cadena);

  • 8/6/2019 14caracteresycadenas-1231097358820148-1

    7/15

    Ejemplos

    char RandomLetra(void){

    return (RandomInteger (A, Z));}

    bool esMayuscula (char ch){

    return (ch >= A && ch = 0 && ch = a && ch

  • 8/6/2019 14caracteresycadenas-1231097358820148-1

    8/15

    Interfaces tiles

  • 8/6/2019 14caracteresycadenas-1231097358820148-1

    9/15

    La interfaz ctype.h

    Contiene un gran nmero de funciones paradeterminar el tipo de carcter, entre lasprincipales tenemos:

    islower(ch) retorna TRUE si el carcter ch es minscula isupper(ch) retorna TRUE si el carcter ch es mayscula

    isalpha(ch) retorna TRUE si ch es un valor alfabtico

    isdigit(ch) retorna TRUE si ch es un dgito

    isalnum(ch) retorna TRUE si ch es un valor alfanumrico

    ispunct(ch) retorna TRUE si ch es un smbolo de puntuacin isspace(ch) retorna TRUE si ch es un carcter en blanco

  • 8/6/2019 14caracteresycadenas-1231097358820148-1

    10/15

    ctype.h: Librera de manejo de caracteres

    Prototype Descriptionintisdigit( int c );

    Returns true ifc is a digit and false otherwise.

    intisalpha( int c );Returns true ifc is a letter and false otherwise.

    intisalnum( int c );Returns true ifc is a digit or a letter and false otherwise.

    intisxdigit( int c );Returns true ifc is a hexadecimal digit character and false otherwise.

    intislower( int c );Returns true ifc is a lowercase letter and false otherwise.

    intisupper( int c );Returns true ifc is an uppercase letter; false otherwise.

    int tolower( int c );Ifc is an uppercase letter, tolower returns c as a lowercase letter. Otherwise, tolower

    returns the argument unchanged.int toupper( int c );

    Ifc is a lowercase letter, toupper returns c as an uppercase letter. Otherwise, toupper

    returns the argument unchanged.intisspace( int c );

    Returns true ifc is a white-space characternewline ('\n'), space (''), form feed

    ('\f'), carriage return ('\r'), horizontal tab ('\t'), or vertical tab ('\v')and false

    otherwiseintiscntrl( int c );

    Returns true ifc is a control character and false otherwise.intispunct( int c );

    Returns true ifc is a printing character other than a space, a digit, or a letter and false

    otherwise.intisprint( int c );

    Returns true value ifc is a printing character including space ('') and false otherwise.

    intisgraph( int c );Returns true ifc is a printing character other than space ( '') and false otherwise.

  • 8/6/2019 14caracteresycadenas-1231097358820148-1

    11/15

    Stdlib.h: Librera de funciones de conversin

    Convierte cadenas de dgitos a enteros yvalores de punto flotante.

    Function prototype Function descriptiondouble atof( const char *nPtr ); Converts the stringnPtrtodouble.

    int atoi( const char *nPtr ); Converts the stringnPtr toint.long atol( const char *nPtr ); Converts the stringnPtrtolong int .double strtod( const char *nPtr, char**endPtr );

    Converts the stringnPtrtodouble.

    long strtol( const char *nPtr, char**endPtr, int base );

    Converts the stringnPtrtolong.

    unsigned long strtoul( constchar*nPtr, char **endPtr, int base ); Converts the string nPtr tounsigned long.

  • 8/6/2019 14caracteresycadenas-1231097358820148-1

    12/15

    stdio.h

    F ctio rotot F ctio d scri tioint getchar( void ); Inputs the next character rom the standard input and re-

    turns it as an integer.

    char *gets( char *s ); Inputs characters rom the standard input into the array s

    until a ne line or end-o - ile character is encountered. A

    terminating nullcharacter is appended to the array.int putchar( int c ); Prints the character stored in c.int puts( const char *s ); Prints the string s ollo ed by a ne line character.int sprintf( char *s, constchar *format, ... );

    quivalent to printf, except the output is stored in the

    array s instead o printing it on the screen.int sscanf( char *s, constchar *format, ... );

    quivalent to scanf, except the input is read rom the array

    s instead o reading it rom the keyboard.

  • 8/6/2019 14caracteresycadenas-1231097358820148-1

    13/15

    String.h: Librera de manipulacin de cadenas

    Incluye funciones para: Manipular cadenas

    Bsqueda en cadenas

    Manejo de tokens

    Determine la longitud de cadenas

    Function prototype Function descriptionchar *strcpy( char*s1, const char *s2 )

    Copies string s2 into array s1. The value o s1is returned.

    char *strncpy( char*s1, const char *s2,size_t n )

    Copies at most n characters o string s2 into array s1. The value o s1is returned.

    char *strcat( char*s1, const char *s2 )

    Appends string s2 to array s1. The irst character o s2 over rites the

    terminating nullcharacter o s1. The value o s1 is returned.char *strncat( char*s1, const char *s2,size_t n )

    Appends at most n characters o string s2 to array s1. The irst

    character o s2 over rites the terminating null character o s1. The

    value o s1 is returned.

  • 8/6/2019 14caracteresycadenas-1231097358820148-1

    14/15

  • 8/6/2019 14caracteresycadenas-1231097358820148-1

    15/15

    Funciones de BsquedaFunction rotot e Function descri tion

    char *strchr( const char *s,int c ); Locates the irst occurrence o characterc in string s. I c is ound, apointer to c in s is returned. ther ise, a NULL pointer is returned.size_t strcspn( const char*s1, const char *s2 ); Determines and returns the length o the initial segment o string s1consisting o characters not contained in strings2.size_t strspn( const char*s1, const char *s2 ); Determines and returns the length o the initial segment o string s1consisting only o characters contained in string s2.char *strpbrk( const char*s1, const char *s2 ); Locates the irst occurrence in string s1 o any character in string s2.I a character rom string s2 is ound, a pointer to the character in

    string s1 is returned. ther ise, a NULL pointer is returned.char *strrchr( const char *s,int c ); Locates the last occurrence o c in string s. I c is ound, a pointer to cin string s is returned. ther ise, a NULL pointer is returned.char *strstr( const char *s1,const char *s2 ); Locates the irst occurrence in string s1 o string s2. I the string isound, a pointer to the string in s1 is returned. ther ise, a NULL

    pointer is returned.char *strtok( char *s1, constchar *s2 ); A sequence o calls to strtok breaks string s1 into tokens logical pieces such as ords in a line o text separated by characters

    contained in string s2. The irst call contains s1 as the irst argument,and subsequent calls to continue tokenizing the same string contain

    NULL as the irst argument. A pointer to the current token is returnedby each call. I there are no more tokens hen the unction is called,

    NULL is returned.