This repository has been archived by the owner on Jun 16, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d92c8dd
commit a46006b
Showing
9 changed files
with
229 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
# Stringhe | ||
Una stringa di caratteri è una sequenza di carattere | ||
|
||
Un letterale stringa è un "oggetto" del tipo: "Hello world" | ||
|
||
## Letterali stringa notevoli - escaping | ||
- "\n" newline, ecc. ecc. | ||
- "" stringa nulla | ||
- "c" -> equivale quasi a 'c' | ||
|
||
## Implementazioni delle stringhe in C\C++ | ||
Le stringhe non sono tipi primitivi, ma sono implementate con array di caratteri terminiati da '\0' (corrisponde al codice 0 della codifica ASCII) | ||
|
||
Si può trovare il tipo astratto `string` nella standard libary, ma non lo utilizzeremo | ||
|
||
## Definizione | ||
`[const] char <identificatore> [<espr-costante>];` | ||
|
||
## Terminatore | ||
|
||
L'ultimo elemento deve essere SEMPRE il terminatore -> deve esserci abbastanza spazio nell'array per contenerlo | ||
|
||
Eg: una stringa di 20 caratteri necessita 21 elementi char per essere memorizzata | ||
Eg: in un array di dimensione 6 byte posso memorizzare al più 5 caratteri | ||
|
||
Tutto ciò che si trova dopo il carattere terminatore deve essere considerato "nullo" oppure casuale. | ||
|
||
Stringa => array di caratteri con terminatore | ||
Non vale il contrario: non sempre un array di caratteri è una stringa -> potrebbe mancare il carattere terminatore | ||
|
||
## Inizializzazione | ||
char nome[] = "Marco"; | ||
char nome[6] = +... | ||
char nome[] = {"M", "a", "r", "c", "o", "\0"}; | ||
char nome[6] = ... | ||
|
||
## Assegnamento | ||
Non è possibile assegnare un letterale stringa (o qualsiasi oggetto di tale tipo) ad un altro oggetto stringa. Bisogna copiare elemento per elemento. | ||
|
||
## Definizione di parola | ||
=> sequenza di caratteri non separati da spazi bianchi (spazio, tab, newline...) | ||
|
||
## I/O di stringhe | ||
cout << _stringa_ stampa tutti gli elementi di un vettore di caratteri fino a che non incontr a il terminatore | ||
|
||
cin >> _stringa_ memorizza la prossima parola presente sul buffer dello stdin all'interno della variabile | ||
|
||
// TODO: link esercizio def_stringa_io.cpp | ||
|
||
## StackSmashing | ||
Vedi programma sopra, inserendo stringa più lunga della dimensione della memoria allocata | ||
|
||
## Errori comuni | ||
``` | ||
char a[10]; | ||
a = "foo"; | ||
``` | ||
|
||
``` | ||
char a[10], b[10]; | ||
a = b; // gli elementi vanno copiati uno ad uno | ||
``` | ||
|
||
## Differenza tra stringhe di singoli caratteri e caratteri | ||
'a' non equivale a "a" | ||
Il primo elemento occupa 1 byte in memoria, il secondo 2b, in quanto è contenuto anche il terminatore all'interno dell'array di caratteri che rappresenta questa stringa in memoria | ||
|
||
// TODO: add link to str_len.cpp | ||
// TODO: add link to mult_str_len.cpp | ||
|
||
W: quando copi il contenuto di una stringa in un'altra ricordati di aggiungere il carattere '\0' nell'ultima posizione | ||
|
||
## Passaggio ad una funzione | ||
Le stringe si passano nello stesso modo degli array (di caratteri). Ricorda che è possibile usare const. In questo caso non è necessario passare la lunghezza delle stringhe (terminatore presente alla fine di essa). | ||
|
||
## Funzioni di libreria | ||
Il linguaggio C/C++ è provvisto di una ricca libreria di funzioni per la gestione delle stringhe | ||
|
||
`<cstring>` oppure `string.h` in C | ||
|
||
- `strcpy(dst, src)` copia la stringa src in dst (adeguatamente grande) | ||
- `strncpy(dst, src)` copia i primi n caratteri di src in dst (adeguatamente grande) | ||
- `strcat(s1, s2)` concatena s1 ed s2; il risultato viene copiato in s1 | ||
- `strcmp(s1, s2)` ritorna: | ||
- 0 s1 == s2 | ||
- > 1 s1 maggiore di s2 secondo l'ordine lessicografico | ||
- < 1 s1 minore di s2 secondo l'ordine lessicografico | ||
|
||
## strcmp() | ||
Viene ritornata la differenza (char di s1 - char di s2) tra il codice ascii del primo carattere differente tra le due stringhe | ||
|
||
// TODO: link conta_car.cpp | ||
// TODO: compito verbosity.cpp | ||
|
||
## Documentazione | ||
www.cppreference.com | ||
|
||
Vedi Null-terminated byte string | ||
|
||
// TODO: link conta_occor2.cpp | ||
// TODO: link copia_stringhe.cpp | ||
|
||
// TODO: per compito implementare RSA su stringhe | ||
// TODO: per esercizi vedi slides |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#include <iostream> | ||
|
||
using namespace std; | ||
|
||
int main() { | ||
char str[] = "Analisi dei requisiti"; | ||
int n = 0; | ||
char c; | ||
|
||
cout << "Carattere: "; cin >> c; | ||
for (int i=0; str[i] != '\0'; i++) | ||
if (str[i] == c) | ||
n++; | ||
|
||
cout << c << " compare " << n << " volte nella stringa \"" << str << "\"" << endl; | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#include <iostream> | ||
#include <cstring> | ||
#include <stdlib.h> | ||
|
||
using namespace std; | ||
|
||
const int MAX_DIM = 100; | ||
|
||
int conta_occorrenze(const char[], char); | ||
|
||
int main() { | ||
char str[MAX_DIM]; | ||
char c; | ||
|
||
cout << "Inserisci la parola in cui cercare: "; cin >> str; | ||
cout << "Stringa: " << str << endl; | ||
cout << "Quale carattere devo cercare? "; cin >> c; | ||
|
||
int occorrenze = conta_occorrenze(str, c); | ||
|
||
cout << c << " compare " << occorrenze << " volte nella stringa \"" << str << "\"" << endl; | ||
return 0; | ||
} | ||
|
||
// case-insensitive occurrencies counter | ||
int conta_occorrenze(const char s[], char c) { | ||
int len = strlen(s); | ||
int counter = 0; | ||
c = tolower(c); | ||
for (int i=0; i<len; i++) { | ||
if (tolower(s[i]) == c) | ||
counter++; | ||
} | ||
return counter; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#include <iostream> | ||
|
||
using namespace std; | ||
|
||
int copia_custom(char dst[], const char src[]) { | ||
int i; | ||
for (i=0; src[i] != '\0'; i++) | ||
dst[i] = src[i]; | ||
dst[i] = '\0'; | ||
return i; | ||
} | ||
|
||
int main() { | ||
char str1[100] = "Prima"; | ||
char str2[100] = "Seconda"; | ||
|
||
cout << str1 << endl; | ||
cout << str2 << endl; | ||
|
||
int l1 = copia_custom(str1, str2); | ||
|
||
cout << str1 << endl; | ||
cout << str2 << endl; | ||
cout << l1 << endl; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#include <iostream> | ||
#include <iomanip> | ||
|
||
using namespace std; | ||
|
||
int main() { | ||
char str[] = "Pippo"; | ||
cout << str << endl; | ||
cin >> str; // L'input di una stringa di dimensioni maggiori di 5 caratteri potrebbe sollevare uno StackSmashing error | ||
// non è comunque possibile leggere una riga intera, neanche utilizzando noskipws | ||
cout << str << endl; | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#include <iostream> | ||
|
||
using namespace std; | ||
|
||
int main() { | ||
char str[100] = "Selva oscura di Dante"; | ||
int len = 0; | ||
|
||
for (int i=0; ; i++, len++) { | ||
cout << str[i]; | ||
if (str[i] == ' ' || str[i] == '\0') { | ||
cout << "\tlen : " << len << endl; | ||
len = 0; | ||
} | ||
if (str[i] == '\0') | ||
break; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#include <iostream> | ||
|
||
using namespace std; | ||
|
||
int main() { | ||
char str[100]; | ||
cin >> str; | ||
int len; | ||
for (len=0; str[len] != 0; len++) ; | ||
cout << str << " è lunga: " << len << endl; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
a | ||
b | ||
c | ||
d | ||
aa | ||
aa | ||
|
||
|
||
sdaga |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,7 @@ int main() { | |
return 1; | ||
} | ||
|
||
fr >> noskipws; | ||
while (cout << c) { | ||
fr >> c; | ||
if (fr.eof()) | ||
|