that_one_nigga_you_k

teste dos indices

Nov 24th, 2023
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.90 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. int main() {
  5.     char mainString[100];
  6.     char substring[20];
  7.  
  8.     // Ler a string principal
  9.     printf("Digite a string principal: ");
  10.     scanf("%99s", mainString);
  11.  
  12.     // Ler a substring
  13.     printf("Digite a substring: ");
  14.     scanf("%19s", substring);
  15.  
  16.     int indices[20];  // Assumindo o número máximo de caracteres na substring como 20
  17.     int count = 0;
  18.  
  19.     // Encontrar e armazenar os índices da primeira ocorrência de cada caractere na substring
  20.     for (int j = 0; j < strlen(substring); j++) {
  21.         char currentChar = substring[j];
  22.         int found = 0;
  23.  
  24.         for (int i = 0; i < strlen(mainString); i++) {
  25.             if (currentChar == mainString[i]) {
  26.                 indices[count++] = i;
  27.                 found = 1;
  28.                 break;
  29.             }
  30.         }
  31.  
  32.         // Se o caractere não for encontrado, definir seu índice como -1
  33.         if (!found) {
  34.             indices[count++] = -1;
  35.         }
  36.     }
  37.  
  38.     // Verificar se a substring está totalmente presente na string principal
  39.     int substringPresent = 1;
  40.     int achadas = 0;
  41.     for (int i = 0; i < count; i++) {
  42.         if (indices[i] == -1) {
  43.             substringPresent = 0;
  44.             break;
  45.         }
  46.     }
  47.  
  48.     // Remover a substring apenas se estiver totalmente presente na string principal
  49.     if (substringPresent) {
  50.         for (int i = 0; i < count; i++) {
  51.             mainString[indices[i]] = '0';
  52.         }
  53.  
  54.         achadas++;
  55.     }
  56.  
  57.    
  58.  
  59.     // Imprimir os índices
  60.     printf("Índices da primeira ocorrência de cada caractere na substring: ");
  61.     for (int i = 0; i < count; i++) {
  62.         printf("%d ", indices[i]);
  63.     }
  64.     printf("\n");
  65.  
  66.     // Imprimir a string principal após a remoção da substring
  67.     printf("%s\n", mainString);
  68.  
  69.     printf("\n\ndá pra fazer %d palavas na string", achadas);
  70.  
  71.     return 0;
  72. }
  73.  
Add Comment
Please, Sign In to add comment