Rešenje 2. zadatka (Python, C#, C++ i C )
Uslovi za završetak
Zadatak može da se radi na više načina, i svaki od delova takođe može da se radi na više načina.
U nastavku su kompletne verzije zadatka sa komentarima liniju po liniju - na jedan od načina
REŠENJE za Python
s = input("Unesite string:\n")
duzina = len(s)
N = duzina // 4
# ODGOVOR 1
print("************** Odgovor 1 **************")
mala = sum(c.islower() for c in s)
velika = sum(c.isupper() for c in s)
cifre = sum(c.isdigit() for c in s)
if mala == velika == cifre:
print("String je uravnotezen.")
else:
print("String nije uravnotezen.")
# ODGOVOR 2
print("************** Odgovor 2 **************")
sekvence = []
for i in range(N):
sek = ""
for j in range(i, duzina, N):
sek += s[j]
sekvence.append(sek)
print(sek)
# ODGOVOR 3
print("************** Odgovor 3 **************")
nove = []
for sek in sekvence:
nova = ""
for i, c in enumerate(sek):
if c.islower():
nova += 'a' if c == 'z' else chr(ord(c) + 1)
elif c.isupper():
nova += 'Z' if c == 'A' else chr(ord(c) - 1)
elif c.isdigit():
nova += str(int(c) + i)
nove.append(nova)
print(nova)
# ODGOVOR 4
print("************** Odgovor 4 **************")
s2 = "".join(nove)
print("s2:", s2)
max_pod = ""
trenutni = ""
for c in s2:
if c.isalpha():
trenutni += c
if len(trenutni) > len(max_pod):
max_pod = trenutni
else:
trenutni = ""
print("Najduzi podniz slova:", max_pod)
print("Duzina:", len(max_pod))
# ODGOVOR 5
print("************** Odgovor 5 **************")
len_s2 = len(s2)
m = 1
while m * m < len_s2:
m += 1
k = 0
for i in range(m):
for j in range(m):
if k < len_s2:
if s2[k].isalpha():
print(ord(s2[k]), end="\t")
else:
print(-int(s2[k]), end="\t")
k += 1
else:
print(0, end="\t")
print()
REŠENJE za C#
using System; using System.Collections.Generic; class Program { // Provera tipa karaktera static bool MaloSlovo(char c) { return c >= 'a' && c <= 'z'; // da li je malo slovo } static bool VelikoSlovo(char c) { return c >= 'A' && c <= 'Z'; // da li je veliko slovo } static bool Cifra(char c) { return c >= '0' && c <= '9'; // da li je cifra } static void Main() { // ========================= // KORAK 1 – unos stringa // ========================= Console.WriteLine("Unesite string:"); string s = Console.ReadLine(); // unos stringa int duzina = s.Length; // dužina stringa int N = duzina / 4; // računamo N // ========================= // ODGOVOR 1 – uravnoteženost // ========================= Console.WriteLine("************** Odgovor 1 **************"); int mala = 0, velika = 0, cifre = 0; // brojači foreach (char c in s) { if (MaloSlovo(c)) mala++; // brojimo mala slova else if (VelikoSlovo(c)) velika++; // brojimo velika else if (Cifra(c)) cifre++; // brojimo cifre } if (mala == velika && velika == cifre) Console.WriteLine("String je uravnotezen."); else Console.WriteLine("String nije uravnotezen."); // ========================= // ODGOVOR 2 – sekvence // ========================= Console.WriteLine("************** Odgovor 2 **************"); List sekvence = new List(); // lista sekvenci for (int i = 0; i < N; i++) // pravimo N sekvenci { string sekvenca = ""; for (int j = i; j < duzina; j += N) // skok za N { sekvenca += s[j]; // dodajemo karakter } sekvence.Add(sekvenca); // čuvamo sekvencu Console.WriteLine(sekvenca); // ispis } // ========================= // ODGOVOR 3 – transformacija // ========================= Console.WriteLine("************** Odgovor 3 **************"); List noveSekvence = new List(); foreach (string sek in sekvence) { string nova = ""; for (int i = 0; i < sek.Length; i++) { char c = sek[i]; if (MaloSlovo(c)) { if (c == 'z') nova += 'a'; // z → a else nova += (char)(c + 1); // sledeće slovo } else if (VelikoSlovo(c)) { if (c == 'A') nova += 'Z'; // A → Z else nova += (char)(c - 1); // prethodno slovo } else if (Cifra(c)) { int cifra = c - '0'; // konverzija u broj nova += (cifra + i).ToString(); // cifra + pozicija } } noveSekvence.Add(nova); Console.WriteLine(nova); } // ========================= // ODGOVOR 4 – s2 i podniz // ========================= Console.WriteLine("************** Odgovor 4 **************"); string s2 = ""; foreach (string sek in noveSekvence) { s2 += sek; // spajamo sekvence } Console.WriteLine("s2: " + s2); string maxPodniz = ""; string trenutni = ""; foreach (char c in s2) { if (char.IsLetter(c)) // ako je slovo { trenutni += c; // dodajemo u trenutni niz if (trenutni.Length > maxPodniz.Length) maxPodniz = trenutni; // čuvamo najduži } else { trenutni = ""; // reset kad naiđe cifra } } Console.WriteLine("Najduzi podniz slova: " + maxPodniz); Console.WriteLine("Duzina: " + maxPodniz.Length); // ========================= // ODGOVOR 5 – matrica // ========================= Console.WriteLine("************** Odgovor 5 **************"); int len = s2.Length; int m = 1; while (m * m < len) m++; // dimenzija matrice int[,] M = new int[m, m]; int k = 0; for (int i = 0; i < m; i++) { for (int j = 0; j < m; j++) { if (k < len) { char c = s2[k]; if (char.IsLetter(c)) M[i, j] = (int)c; // ASCII vrednost else M[i, j] = -(c - '0'); // negativna cifra k++; } else { M[i, j] = 0; // popunjavanje nulama } Console.Write(M[i, j] + "\t"); } Console.WriteLine(); } } }
REŠENJE za C
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
int main() {
char s[100];
printf("Unesite string:\n");
scanf("%s", s);
int duzina = strlen(s);
int N = duzina / 4;
// =========================
// ODGOVOR 1
// =========================
printf("************** Odgovor 1 **************\n");
int mala = 0, velika = 0, cifre = 0;
for (int i = 0; i < duzina; i++) {
if (islower(s[i])) mala++;
else if (isupper(s[i])) velika++;
else if (isdigit(s[i])) cifre++;
}
if (mala == velika && velika == cifre)
printf("String je uravnotezen.\n");
else
printf("String nije uravnotezen.\n");
// =========================
// ODGOVOR 2
// =========================
printf("************** Odgovor 2 **************\n");
char sekvence[10][10];
for (int i = 0; i < N; i++) {
int k = 0;
for (int j = i; j < duzina; j += N) {
sekvence[i][k++] = s[j];
}
sekvence[i][k] = '\0';
printf("%s\n", sekvence[i]);
}
// =========================
// ODGOVOR 3
// =========================
printf("************** Odgovor 3 **************\n");
char nove[10][20];
for (int i = 0; i < N; i++) {
int len = strlen(sekvence[i]);
for (int j = 0; j < len; j++) {
char c = sekvence[i][j];
if (islower(c)) {
nove[i][j] = (c == 'z') ? 'a' : c + 1;
}
else if (isupper(c)) {
nove[i][j] = (c == 'A') ? 'Z' : c - 1;
}
else if (isdigit(c)) {
int cifra = c - '0';
int vrednost = cifra + j;
nove[i][j] = vrednost + '0'; // pretvaranje u karakter
}
}
nove[i][len] = '\0';
printf("%s\n", nove[i]);
}
// =========================
// ODGOVOR 4
// =========================
printf("************** Odgovor 4 **************\n");
char s2[200] = "";
for (int i = 0; i < N; i++) {
strcat(s2, nove[i]);
}
printf("s2: %s\n", s2);
char max[100] = "", trenutni[100] = "";
int k = 0;
for (int i = 0; i < strlen(s2); i++) {
if (isalpha(s2[i])) {
trenutni[k++] = s2[i];
trenutni[k] = '\0';
if (strlen(trenutni) > strlen(max))
strcpy(max, trenutni);
} else {
k = 0;
trenutni[0] = '\0';
}
}
printf("Najduzi podniz slova: %s\n", max);
printf("Duzina: %d\n", (int)strlen(max));
// =========================
// ODGOVOR 5
// =========================
printf("************** Odgovor 5 **************\n");
int len = strlen(s2);
int m = 1;
while (m * m < len) m++;
int M[10][10];
int idx = 0;
for (int i = 0; i < m; i++) {
for (int j = 0; j < m; j++) {
if (idx < len) {
if (isalpha(s2[idx]))
M[i][j] = (int)s2[idx];
else
M[i][j] = -(s2[idx] - '0');
idx++;
} else {
M[i][j] = 0;
}
printf("%d\t", M[i][j]);
}
printf("\n");
}
return 0;
}
REŠENJE za C++
#include <iostream>
#include <string>
#include <vector>
#include <cctype>
using namespace std;
int main() {
string s;
cout << "Unesite string:\n";
cin >> s;
int duzina = s.length();
int N = duzina / 4;
// ODGOVOR 1
cout << "************** Odgovor 1 **************\n";
int mala = 0, velika = 0, cifre = 0;
for (char c : s) {
if (islower(c)) mala++;
else if (isupper(c)) velika++;
else if (isdigit(c)) cifre++;
}
if (mala == velika && velika == cifre)
cout << "String je uravnotezen.\n";
else
cout << "String nije uravnotezen.\n";
// ODGOVOR 2
cout << "************** Odgovor 2 **************\n";
vector<string> sekvence;
for (int i = 0; i < N; i++) {
string sek = "";
for (int j = i; j < duzina; j += N)
sek += s[j];
sekvence.push_back(sek);
cout << sek << endl;
}
// ODGOVOR 3
cout << "************** Odgovor 3 **************\n";
vector<string> nove;
for (string sek : sekvence) {
string nova = "";
for (int i = 0; i < sek.length(); i++) {
char c = sek[i];
if (islower(c))
nova += (c == 'z') ? 'a' : c + 1;
else if (isupper(c))
nova += (c == 'A') ? 'Z' : c - 1;
else if (isdigit(c))
nova += char((c - '0' + i) + '0');
}
nove.push_back(nova);
cout << nova << endl;
}
// ODGOVOR 4
cout << "************** Odgovor 4 **************\n";
string s2 = "";
for (string sek : nove)
s2 += sek;
cout << "s2: " << s2 << endl;
string max = "", trenutni = "";
for (char c : s2) {
if (isalpha(c)) {
trenutni += c;
if (trenutni.length() > max.length())
max = trenutni;
} else {
trenutni = "";
}
}
cout << "Najduzi podniz slova: " << max << endl;
cout << "Duzina: " << max.length() << endl;
// ODGOVOR 5
cout << "************** Odgovor 5 **************\n";
int len = s2.length();
int m = 1;
while (m * m < len) m++;
int k = 0;
for (int i = 0; i < m; i++) {
for (int j = 0; j < m; j++) {
if (k < len) {
if (isalpha(s2[k]))
cout << (int)s2[k] << "\t";
else
cout << -(s2[k] - '0') << "\t";
k++;
} else {
cout << "0\t";
}
}
cout << endl;
}
return 0;
}
Ne morate imati instaliran program da biste radili (i proverili) zadatak.
Online kompajleri:
- za Python, na primer: https://www.programiz.com/python-programming/online-compiler/
- za C# -> https://www.programiz.com/csharp-programming/online-compiler/
- za C -> https://www.programiz.com/c-programming/online-compiler/
- za C++ -> https://www.programiz.com/cpp-programming/online-compiler/
Ovo nije jedino online okruženje u kojem se može raditi - ima ih dosta, a vi koristite ono koje vam najviše odgovara.
...
Poslednja izmena: среда, 29. април 2026, 13:19