<cstring>
String mutation functions:
strcpy()
- copy one string to anotherstrncpy()
- copy n bytes from one string to another,
from src are copied or nulls are paddedstrcat()
- concatenates string at the end of
anotherstrncat()
- concatenates n bytes of one string to
anotherMemory changing functions:
memset()
- filles array with some byteFunctions for conversion string to numbers:
atof()
- converts string to decimal numberatoi()
- converts string to integer numberString checking functions:
strlen()
- returns the length of the stringstrcmp()
- compares two stringsstrncmp()
- compares n bytes from two stringsstrchr()
- finds the first occurrence of given
character in some stringstrrchr()
- finds the last occurrence of given
character not in a set of charsstrspn()
- finds the first occurrence of given
character in some string in a given set of charsstrpbrk()
- finds the first occurrence of given
character in some string in a given set of charsstrstr()
- finds the first occurrence of string in
other stringFunctions for characters:
isalnum()
- checks if the character is alphanumeric
(letter or number)isalpha()
- checks if the character is letteriscntrl()
- checks if the character is controlisdigit()
- checks if the character is digitisxdigit()
- checks if the character is hex digitisprint()
- checks if the character is printableispunct()
- checks if the character is punctuationisspace()
- checks if the character is printableislower()
- checks if the character is lowercaseisupper()
- checks if the character is uppercasetolower()
- converts to lowercasetoupper()
- converts to uppercaseisgraph()
- checks if the character has local graphic
representationWrite a function that will find how many times a given character occurs in given string.
Example
For the string
HELLO FINKI
characterL
occurs 2 times.
#include <iostream>
using namespace std;
int count_char(char str[], char c) {
int vkupno = 0;
for (int i = 0; str[i] != '\0'; i++) {
if (str[i] == c) vkupno++;
}
return vkupno;
}
int main() {
int MAX = 100;
char s[MAX], c;
.getline(s, MAX);
cin>> c;
cin << count_char(s, c);
cout return 0;
}
Write a function that will return the length of a string.
Also write a recursive solution.
Example
For string
zdravo!
it should return: 7
#include <iostream>
using namespace std;
int length(char s[]) {
int len = 0;
for (int i = 0; s[i] != '\0'; i++)
++;
lenreturn len;
}
int length_r(char *s) { // same as char s[]
if (*s == '\0') // same as if (s[0] == '\0')
return 0;
return 1 + length_r(s + 1);
}
int main() {
int MAX = 100;
char s[MAX];
.getline(s, MAX);
cin<< "Dolzhina: " << length(s) << " i " << length_r(s);
cout return 0;
}
Write a program that will print substring from given string, determined with the position and the length as parameters read from SI. The substring starts from the character on the position counted from left to right.
Example
For the string:
banana
position: 2
length: 4
the output is: nana
#include <iostream>
#include <cstring>
using namespace std;
int main() {
int MAX = 100;
char s[MAX], dest[MAX];
int pozicija, dolzhina;
.getline(s, MAX);
cin>> pozicija >> dolzhina;
cin
if (pozicija <= strlen(s)) {
(dest, s + pozicija - 1, dolzhina);
strncpy[dolzhina] = '\0';
dest<< "Rezultat: " << dest << endl;
cout } else
<< "Nevaliden vnes, prochitaniot string ima samo %d znaci.\n" << strlen(s);
cout return 0;
}
Write a function that will check if one string is substring of some other string.
Example
face
is substring of Please faceAbook
#include <iostream>
#include <cstring>
using namespace std;
int podstring(char *s1, char *s2) {
int d1 = strlen(s1);
int d2 = strlen(s2);
if (d1 > d2)
return 0;
for (int i = 0; i <= d2 - d1; i++)
if (strncmp(s1, s2 + i, d1) == 0)
return 1;
return 0;
}
int main() {
int MAX = 100;
char s1[MAX], s2[MAX];
.getline(s1, MAX);
cin.getline(s2, MAX);
cinif (podstring(s1, s2))
<< s1 << " e podstring na " << s2 << endl;
cout else
<< s1 << " NE e podstring na " << s2 << endl;
cout return 0;
}
Write a function that will check if given string is palindrome. Palindrome is a string that is read same from left to right and from right to left.
Examples for palindromes
dovod
ana
kalabalak
#include <iostream>
#include <cstring>
using namespace std;
int e_palindrom(char str[]) {
int n = strlen(str);
for (int i = 0; i < n / 2; i++)
if (str[i] != str[n - 1 - i])
return 0;;
return 1;
}
// REKURZIVNO
int e_pal(char str[], int start, int end) {
if (start >= end) return 1;
if (str[start] == str[end])
return e_pal(str, start + 1, end - 1);
return 0;
}
int main() {
int MAX = 100;
char s[MAX];
.getline(s, MAX);
cin<< s << " ";
cout if (e_pal(s, 0, strlen(s) - 1))
("e palindrom.");
printfelse
("NE e palindrom.");
printfreturn 0;
}
Write a function that will check if given sentence is a palindrome. Ignore the empty spaces, punctuations characters and the case of letters.
Examples for sentences palindromes
Jadejne i pienje daj!
A man, a plan, a canal, Panama.
Never odd or even.
Rise to vote sir!
Write a function that for a given string will if it’s complex enough to become a password. Every password must have at least one letter, one digit and one special character.
Example
zdr@v0!
is a valid password.
zdravo
is not a valid password.
#include <iostream>
#include <cctype>
using namespace std;
int e_validna_lozinka(char str[]) {
int bukvi = 0, cifri = 0, spec = 0;
for (int i = 0; str[i] != '\0'; i++) {
if (isalpha(str[i]))
++;
bukvielse if (isdigit(str[i]))
++;
cifrielse
++;
spec}
return (bukvi > 0 && cifri > 0 && spec > 0);
}
int main() {
int MAX = 100;
char s[MAX];
.getline(s, MAX);
cin
<< s << " ";
cout if (e_validna_lozinka(s))
<< "e validna lozinka.";
cout else
<< "NE e validna lozinka.";
cout return 0;
}
Write a function that for will change the case of the letters and will remove all digits and special characters.
Example
For the string:
0v@ePr1m3R
the result should be:
VEpRMr
#include <iostream>
#include <cctype>
using namespace std;
void filter(char str[]) {
int i = 0, j = 0;
while (str[i] != '\0') {
if (isalpha(str[i])) {
if (islower(str[i]))
[j] = toupper(str[i]);
strelse if (isupper(str[i]))
[j] = tolower(str[i]);
str++;
j}
++;
i}
[j] = '\0';
str}
int main() {
int MAX = 100;
char s[MAX];
.getline(s, MAX);
cin
(s);
filter<< s << endl;
cout return 0;
}
Write a function that will trim a string (remove blanks at front and end of string).
Example
For the string:
" make trim "
the output should be:
"make trim"
#include <iostream>
#include <cctype>
using namespace std;
void trim(char s[]) {
int spaces = 0;
while (isspace(s[spaces])) {
++;
spaces}
int i;
for (i = 0; s[i + spaces] != '\0'; i++) {
[i] = s[i + spaces];
s}
[i] = '\0';
swhile (i > 0 && isspace(s[i - 1])) {
--;
i[i] = '\0';
s}
}
int main() {
int MAX = 100;
char s[MAX];
.getline(s, MAX);
cin<< "[" << s << "] -> ";
cout (s);
trim<< "[" << s << "]";
cout return 0;
}