-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring_utils.cpp
More file actions
151 lines (140 loc) · 3.66 KB
/
string_utils.cpp
File metadata and controls
151 lines (140 loc) · 3.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/**
* Purpose: basic utilities for strings
* Author: Emanuele Rizzolo
* Class: 3XIN
* Date: 2023/01/18
* Note:
*/
#include <iostream>
using namespace std;
const bool DEBUG = true;
int strlen(const char s[]);
// nota char* fun(...) per uniformità con libreria cstring
char *strcpy(char d[], const char s[]);
char *strcat(char d[], const char s[]);
char *strupper(char d[]);
char *strrev(char d[]);
char *strhide(char d[]);
int strcmp(const char *lhs, const char *rhs);
char *strreverse(char *dst);
// main function
int main(int argc, char *argv[])
{
char frase[] = "La vita e' bella";
cout << frase << endl;
cout << "Lunghezza di frase = " << strlen(frase) << endl;
char stringa[20];
strcpy(stringa, frase);
cout << stringa << endl;
cout << "Lunghezza di stringa = " << strlen(stringa) << endl;
cout << strcpy(stringa, frase) << endl;
char sentence[100] = "La vita e' ";
strcat(sentence, "fantastica!");
cout << sentence << endl;
cout << "Lunghezza di sentence = " << strlen(sentence) << endl;
cout << strupper(sentence) << endl;
cout << strcmp("uguale", "uguale") << endl;
cout << strcmp("minore", "z") << endl;
cout << strcmp("z", "maggiore") << endl;
cout << strreverse(sentence) << endl;
// NOTA: dichiarare le funzioni con char* consente cose del tipo:
char nome[] = "Emanuele";
char cognome[] = "Rizzolo";
char saluto[] = "Buongiorno";
char output[200];
// piuttosto illeggibile, mi pare ...
cout << strcat(strcat(strcat(strcat(strcat(saluto, " "), nome), " "), cognome), "!") << endl;
// successful termination
return 0;
}
/// @brief Lunghezza di una stringa
/// Si assume che sia correttamente terminata ...
/// i.e. la sentinella esiste ed è al suo posto
/// @param s la stringa di cui si vuole la lunghezza
/// @return la lunghezza della stringa specificata
int strlen(const char s[])
{
int len = 0;
while (s[len] != 0)
{
++len;
}
return len;
}
/// @brief Copia s in d
/// @param d stringa destinazione
/// @param s stringa sorgente
/// @return d
char *strcpy(char d[], const char s[])
{
int i = 0;
while (s[i] != 0)
{
d[i] = s[i];
i++; // next char
}
d[i] = s[i]; // terminator
return d;
}
/// @brief Concatena s in d
/// @param d stringa destinazione
/// @param s stringa sorgente
/// @return d
char *strcat(char d[], const char s[])
{
int len = strlen(d);
int i = 0;
while (s[i] != 0)
{
d[len + i] = s[i];
i++; // next char
}
d[len + i] = s[i]; // terminator
return d;
}
/// @brief Converte d in maiuscolo
/// @param d stringa destinazione
/// @return d
char *strupper(char d[])
{
int len = 0;
while (d[len] != 0)
{
if (('a' <= d[len]) && (d[len] <= 'z'))
{
d[len] += 'A' - 'a';
}
++len;
}
return d;
}
/// @brief Confronta lhs e rhs (lhs confronto rhs)
/// @param lhs stringa left hand side
/// @param rhs stringa right hand side
/// @return <=> 0 a seconda che lhs <=> rhs
int strcmp(const char *lhs, const char *rhs)
{
int len = 0;
while ((lhs[len] == rhs[len]) && (lhs[len] != 0))
{
++len;
}
return lhs[len] - rhs[len];
}
/// @brief Rovescia
/// @param d stringa destinazione
/// @return d
char *strreverse(char *d)
{
int l = 0, r = strlen(d) - 1;
while (l < r)
{
swap(d[l], d[r]); // system swap ...
// char temp = d[l];
// d[l] = d[r];
// d[r] = temp;
++l;
--r;
}
return d;
}