◐ Shell
clean mode source ↗

std::strchr - cppreference.com

De cppreference.com

Esta página se ha traducido por ordenador/computador/computadora de la versión en inglés de la Wiki usando Google Translate.

La traducción puede contener errores y palabras aparatosas/incorrectas. Planea sobre el texto para ver la versión original. Puedes ayudar a corregir los errores y mejorar la traducción. Para instrucciones haz clic aquí.

Definido en el archivo de encabezado <cstring>

const char *strchr( const char *str, int ch );

  char *strchr( char *str, int ch );

Busca la primera aparición del carácter ch de la cadena de bytes apuntada por str .

Original:

Finds the first occurrence of the character ch in the byte string pointed to by str.

The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

Parámetros

str -

puntero a la cadena de bytes de terminación nula a analizar

Original:

pointer to the null-terminated byte string to be analyzed

The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

ch -

carácter que desea buscar

Original:

character to search for

The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

Valor de retorno

Puntero al carácter encontrado en str o NULL si no se encuentra dicho carácter .

Original:

Pointer to the found character in str, or NULL if no such character is found.

The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

Ejemplo

#include <cstring>
#include <iostream>

int main()
{
    const char* str = "Try not. Do, or do not. There is no try.";
    char objetivo = 'T';
    const char* resultado = str;

    while ((resultado = std::strchr(resultado, objetivo)) != nullptr)
    {
        std::cout << "Encontrado '" << objetivo
                  << "' comenzando en '" << resultado << "'\n";

        // Incrementar resultado, en otro caso encontraremos objetivo en la misma ubicación
        ++resultado;
    }
}

Salida:

Encontrado 'T' comenzando en 'Try not. Do, or do not. There is no try.'
Encontrado 'T' comenzando en 'There is no try.'

Ver también