Untitled

mail@pastecode.io avatar
unknown
plain_text
6 months ago
3.8 kB
2
Indexable
Never
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define max 100

void afisare_caractere(char *sir, int n) {
	printf("Caracterele din sir sunt: ");
	for (int i = 0; i < n; i++) {
		printf("%c ", sir[i]);
	}
	printf("\n");
}

void afisare_alfabetica(char *sir, int n) {
	char ssir[max];
	strcpy(ssir, sir);
	printf("Caracterele in ordine alfabetica sunt: %s\n", ssir);
	for (int i = 0; i < n - 1; i++) {
		for (int j = i + 1; j < n; j++) {
			if (ssir[i] > ssir[j]) {
				char temp = ssir[i];
				ssir[i] = ssir[j];
				ssir[j] = temp;
			}
		}
	}
	printf("%s\n", ssir);
}

void afisare_inversalfa(char *sir, int n) {
	char ssir[max];
	strcpy(ssir, sir);
	printf("Caracterele in ordine invers alfabetica: %s\n", ssir);
	for (int i = 0; i < n - 1; i++) {
		for (int j = i + 1; j < n; j++) {
			if (ssir[i] < ssir[j]) {
				char temp = ssir[i];
				ssir[i] = ssir[j];
				ssir[j] = temp;
			}
		}
	}
	printf("%s\n", ssir);
}

void afisare_vocale(char *sir, int n) {
	int c = 0;
	for (int i = 0; i < n; i++) {
		if (sir[i] == 'a' || sir[i] == 'e' || sir[i] == 'i' || sir[i] == 'o' || sir[i] == 'u') {
			c++;
		}
	}
	printf("Numarul de vocale in sir: %d\n", c);
}

void copiere_ascii(char *sir, int n) {
	int ac[max];
	for (int i = 0; i < n; i++) {
		ac[i] = (int)sir[i];
		printf("%c: %d\n", sir[i], ac[i]);
	}
}

void calcul_asciibinar(char *sir, int n) {
	int s = 0;
	for (int i = 0; i < n; i++) {
		s += (int)sir[i];
	}
	printf("Suma reprezentarilor ASCII: %d\n", s);

	printf("Reprezentarea binara a sumei: ");
	while (s > 0) {
		int r = s % 2;
		printf("%d", r);
		s = s / 2;
	}
	printf("\n");
}

void cuvant_frecvent(char *sir, int n) {
	int maxc = 0;
	char cf = sir[0]; // Inițializare cu primul caracter
	for (int i = 0; i < n - 1; i++) {
		int c = 1;
		for (int j = i + 1; j < n; j++) {
			if (sir[i] == sir[j]) {
				c++;
			}
		}
		if (c > maxc) {
			maxc = c;
			cf = sir[i];
		}
	}
	printf("Cel mai frecvent caracter din sir: %c\n", cf);
}

void constructie_cuvant(char *sir, int n) {
	char w[max];
	int wl = 0;
	for (int i = 0; i < n; i += 2) {
		w[wl++] = sir[i];
	}
	w[wl] = '\0';
	printf("Cuvantul format: %s\n", w);
}

void stergere_ucaracter(char *sir, int *n) {
	(*n)--;
	sir[*n] = '\0';
	printf("Sirul dupa stergerea ultimului caracter: %s\n", sir);
}

int main() {
	char sir[max];
	int n = 0;

	sir[0] = 'e';
	n = 1;

	printf("Introduceti litere mici, litere mari sau'e'= sir incheiat: \n");

	char lm;
	while (1) {
		scanf(" %c", &lm);
		if (lm == 'e') {
			break;
		}
		if (lm >= 'a' && lm <= 'z') {
			sir[n++] = lm;
		}
		else {
			printf("Pentru a opri introducerea, apasati 'e' sau litera 'E'.\n");
		}
	}
	sir[n] = '\0';

	int optiune;
	do {
		printf("1.Afisare caractere\n");
		printf("2.Afisare in ordine alfabetica\n");
		printf("3.Afisare in ordine invers alfabetica\n");
		printf("4.Numara vocale\n");
		printf("5.Reprezentare ASCII\n");
		printf("6.Suma reprezentarilor ASCII si reprezentarea binara a acesteia\n");
		printf("7.Cel mai frecvent caracter\n");
		printf("8.Constructie cuvant din fiecare pozitie\n");
		printf("9.Stergere ultimului caracter\n");
		printf("0.Iesire\n");
		printf("Introduceti optiunea: ");
		scanf("%d", &optiune);

		switch (optiune) {
		case 1:
			afisare_caractere(sir, n);
			break;
		case 2:
			afisare_alfabetica(sir, n);
			break;
		case 3:
			afisare_inversalfa(sir, n);
			break;
		case 4:
			afisare_vocale(sir, n);
			break;
		case 5:
			copiere_ascii(sir, n);
			break;
		case 6:
			calcul_asciibinar(sir, n);
			break;
		case 7:
			cuvant_frecvent(sir, n);
			break;
		case 8:
			constructie_cuvant(sir, n);
			break;
		case 9:
			stergere_ucaracter(sir, &n);
			break;
		case 0:
			printf("Iesire\n");
			break;
		default:
			printf("Optiune gresita.\n");
			break;
		}
	} while (optiune != 0);

	return 0;
}
Leave a Comment