Programa C para remover todos os caracteres em uma string, exceto alfabetos

Neste exemplo, você aprenderá a remover todos os caracteres de uma string inserida pelo usuário, exceto os alfabetos.

Para entender este exemplo, você deve ter conhecimento dos seguintes tópicos de programação C:

  • Matrizes C
  • Strings de programação C
  • C para Loop
  • C while e do … while Loop

Remover caracteres em strings, exceto alfabetos

 #include int main() ( char line(150); printf("Enter a string: "); fgets(line, sizeof(line), stdin); // take input for (int i = 0, j; line(i) != ''; ++i) ( // enter the loop if the character is not an alphabet // and not the null character while (!(line(i)>= 'a' && line(i) = 'A' && line(i) <= 'Z') && !(line(i) == '')) ( for (j = i; line(j) != ''; ++j) ( // if jth element of line is not an alphabet, // assign the value of (j+1)th element to the jth element line(j) = line(j + 1); ) line(j) = ''; ) ) printf("Output String: "); puts(line); return 0; )

Resultado

 Insira uma string: p2'r-o@gram84iz./ String de saída: programiz 

Este programa recebe uma entrada de string do usuário e armazena na variável de linha. Em seguida, um forloop é usado para iterar os caracteres da string.

Se o caractere em uma string não for um alfabeto, ele será removido da string e a posição dos caracteres restantes será deslocada 1 posição para a esquerda.

Artigos interessantes...