Created
November 3, 2019 06:07
-
-
Save parzibyte/a8f6c558476b23cbd63ff5cce4229492 to your computer and use it in GitHub Desktop.
String in substring ANSI C | https://parzibyte.me/blog
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Saber si cadena tiene subcadena (u otra cadena) en C | |
* | |
* @author parzibyte | |
* @see https://parzibyte.me/blog | |
* */ | |
#include <stdio.h> | |
#include <string.h> | |
int main(void) { | |
// Cuando no existe | |
if (strstr("parzibyte.me", "www") != NULL) { | |
printf("Existe www dentro de parzibyte.me\n"); | |
} else { | |
printf("No existe www dentro deu parzibyte.me\n"); | |
} | |
// Cuando sí existe y usando variables | |
char *pajar = "Hola mundo"; | |
char *aguja = "mundo"; | |
if (strstr(pajar, aguja) != NULL) { | |
printf("Existe %s dentro de %s\n", aguja, pajar); | |
} else { | |
printf("No existe %s dentro de %s\n", aguja, pajar); | |
} | |
char *nombre = "Luis Cabrera Benito"; | |
// Saber si cadena tiene carácter | |
char caracter = 'L'; | |
// Convertir a cadena | |
char caracterEnCadena[] = {caracter, '\0'}; | |
// Después usamos strstr como lo hacemos siempre | |
// Buscar si "L" está en "Luis Cabrera Benito" | |
if (strstr(nombre, caracterEnCadena) != NULL) { | |
printf("Existe %s dentro de %s\n", caracterEnCadena, nombre); | |
} else { | |
printf("No existe %s dentro de %s\n", caracterEnCadena, nombre); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment