Šajā piemērā jūs iemācīsities noņemt visas rakstzīmes no lietotāja ievadītas virknes, izņemot alfabētus.
Lai saprastu šo piemēru, jums vajadzētu būt zināšanām par šādām C programmēšanas tēmām:
- C Masīvi
- C programmēšanas virknes
- C cilnei
- C kamēr un dari … kamēr Loop
Noņemiet rakstzīmes virknē, izņemot alfabētus
#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; )
Rezultāts
Ievadiet virkni: p2'r-o@gram84iz./ Output String: programiz
Šī programma ņem no lietotāja virknes ievadi un saglabā līnijas mainīgajā. Pēc tam for
tiek izmantota cilpa, lai atkārtotu virknes rakstzīmes.
Ja rakstzīme virknē nav alfabēts, tā tiek noņemta no virknes un atlikušo rakstzīmju pozīcija tiek pārvietota pa kreisi par 1 pozīciju.