Saturday, November 21, 2015

Limbajul C. Exercitii clasice cu siruri de caractere. MyStrcmp si eliminarea asa-numitelor white-spaces

1. Clasicul MyStrcmp

Doua siruri de caractere sunt "egale" daca au exact aceleasi caractere dispuse in aceeasi ordine.

O functie testeaza aceasta "egalitate" si intoarce zero in caz ca se verifica egalitatea.

Daca sir1 < sir2, adica daca verificarea, pentru un caracter oarecare, incepand cu primul, a codurilor ASCII, satisface inegalitatea, functia intoarce -1.

In caz contrar, intoarce +1.


Codul si printscreen-ul:

#include <stdio.h>

int MyStrcmp(char *s1, char *s2);

void FeedString(char *s); // in loc de functiile predefinite gets si scanf

int main()
{
            char s1[31], s2[31];// vectori de max 30 de caractere + terminatorul de sir
            int check;
            puts("Introduceti primul sir, s1. Incheiati cu zero.");
            FeedString(s1);
            puts("Introduceti al doilea sir, s2. Incheiati cu zero.");
            FeedString(s2);
            check = MyStrcmp(s1, s2);
            if(!check) printf("\nSirurile sunt egale.\n");
            else if(check < 0) printf("\nSirul %s < %s.\n", s1, s2);
            else printf("\nSirul %s > %s.\n", s1, s2);
            return 0;
}

int MyStrcmp(char *s1, char *s2)
{
            int l1, l2, i;
            i = l1 = l2 = 0;
            //De fapt, se poate renunta la portiunea de cod
            //pentru calculul lungimii sirurilor si la
            //luarea deciziei pe baza compararii lungimilor.
            //Rezolvarea a fost scrisa astfel pentru a ilustra
            //prioritatile diferite ale operatorului de indexare
            //si a celui de incrementare postfix
            while(s1[l1++] != '\0');
            l1--;
            while(s2[l2++] != '\0');
            l2--;
            if(l1 != l2)
            {
                        for(; i < (l1 < l2? l2 : l1); i++)
                        {
                                    if(s1[i] == s2[i]) continue;
                                    else if(s1[i] < s2[i]) return -1;
                                    else return 1;
                        }
                       
            }
            else
            {
                        i = 0;
                        while(s1[i] == s2[i]) i++;
                        i;
                        if(i - 1 == l1) return 0;
                        else if(s1[i] < s2[i]) return -1;
                        else return 1;
            }                      
                       
}

void FeedString(char *s)
{
            int i = 0;
            char c = '\0';
            while((c = getchar()) != '0')
            {
                        s[i] = c;
                        if(i == 29)
                        {
                                    s[i + 1] = '\0';
                                    _flushall();
                                    break;
                        }
                        i++;
                        s[i] = '\0';
            }
            _flushall();

}


2. Clasicul program de eliminare a caracterelor white-space dintr-un sir.

Se scrie o functie de eliminare a unui caracter white-space, fie el spatiu (ASCII 32), tab (ASCII 9) sau new line ('\n'. Compilatorul va efectua translatia line feed + carriage return, coduri ASCII 10 si 13). 

Odata caracterul 'alb' eliminat, se invoca in mod convenabil, intr-o bucla, functia pana la elimitarea tuturor caracterelor white-space.

Codul si printscreen-ul:

#include <stdio.h>

void EliminWhite(char tab[], int n, int poz);

void FeedString(char *p);

int main()
{
            char tab[81];
    int i = 0, l = 0;
            puts("Introduceti un sir de max. 80 de caractere terminat prin 0.\nPoate contine whitespaces. Le vom elimina!\n");
            FeedString(tab);
            while(tab[l++] != '\0');
            l--;
            while(tab[i++] != '\0') if(tab[i - 1] == '\t' || tab [i - 1] == '\n' || tab[i - 1] == ' ') EliminWhite(tab, l, i - 1);
            printf("\nAm elimitat white-spaces si a rezultat sirul:\n\n%s\n", tab);
            return 0;
}

void EliminWhite(char tab[], int n, int poz)
{
            int i;
            for(i = poz; i < n; i++) tab[i] = tab[i + 1];
           
}

void FeedString(char *p)
{
            int i = 0;
            char c = '\0';
            while((c = getchar()) != '0')
            {
                        p[i] = c;
                        if(i == 80)
                        {
                                    p[i + 1] = '\0';
                                    _flushall();
                                    break;
                        }
                        p[i + 1] = '\0';
                        i++;
            }
            _flushall();

}


No comments:

Post a Comment