Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- int main() {
- char mainString[100];
- char substring[20];
- // Ler a string principal
- printf("Digite a string principal: ");
- scanf("%99s", mainString);
- // Ler a substring
- printf("Digite a substring: ");
- scanf("%19s", substring);
- int indices[20]; // Assumindo o número máximo de caracteres na substring como 20
- int count = 0;
- // Encontrar e armazenar os índices da primeira ocorrência de cada caractere na substring
- for (int j = 0; j < strlen(substring); j++) {
- char currentChar = substring[j];
- int found = 0;
- for (int i = 0; i < strlen(mainString); i++) {
- if (currentChar == mainString[i]) {
- indices[count++] = i;
- found = 1;
- break;
- }
- }
- // Se o caractere não for encontrado, definir seu índice como -1
- if (!found) {
- indices[count++] = -1;
- }
- }
- // Verificar se a substring está totalmente presente na string principal
- int substringPresent = 1;
- int achadas = 0;
- for (int i = 0; i < count; i++) {
- if (indices[i] == -1) {
- substringPresent = 0;
- break;
- }
- }
- // Remover a substring apenas se estiver totalmente presente na string principal
- if (substringPresent) {
- for (int i = 0; i < count; i++) {
- mainString[indices[i]] = '0';
- }
- achadas++;
- }
- // Imprimir os índices
- printf("Índices da primeira ocorrência de cada caractere na substring: ");
- for (int i = 0; i < count; i++) {
- printf("%d ", indices[i]);
- }
- printf("\n");
- // Imprimir a string principal após a remoção da substring
- printf("%s\n", mainString);
- printf("\n\ndá pra fazer %d palavas na string", achadas);
- return 0;
- }
Add Comment
Please, Sign In to add comment