Structured programming

Auditory Exercise 12 - Strings


1. Remainders from lectures

1.1. String functions from <cstring>

String mutation functions:

Memory changing functions:

Functions for conversion string to numbers:

String checking functions:

1.2. Functions for characters from the library

Functions for characters:

2. Problems

2.1. Problem 1

Write a function that will find how many times a given character occurs in given string.

Example

For the string

HELLO FINKI

characterLoccurs 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;
    cin.getline(s, MAX);
    cin >> c;
    cout << count_char(s, c);
    return 0;
}

2.2. Problem 2

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++)
        len++;
    return 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];
    cin.getline(s, MAX);
    cout << "Dolzhina: " << length(s) << " i " << length_r(s);
    return 0;
}

2.3. Problem 3

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;

    cin.getline(s, MAX);
    cin >> pozicija >> dolzhina;

    if (pozicija <= strlen(s)) {
        strncpy(dest, s + pozicija - 1, dolzhina);
        dest[dolzhina] = '\0';
        cout << "Rezultat: " << dest << endl;
    } else
        cout << "Nevaliden vnes, prochitaniot string ima samo %d znaci.\n" << strlen(s);
    return 0;
}

2.4. Problem 4

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];
    cin.getline(s1, MAX);
    cin.getline(s2, MAX);
    if (podstring(s1, s2))
        cout << s1 << " e podstring na " << s2 << endl;
    else
        cout << s1 << " NE e podstring na " << s2 << endl;
    return 0;
}

2.5. Problem 5

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];
    cin.getline(s, MAX);
    cout << s << " ";
    if (e_pal(s, 0, strlen(s) - 1))
        printf("e palindrom.");
    else
        printf("NE e palindrom.");
    return 0;
}

2.6. Problem 5-a (homework)

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!

2.7. Problem 6

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]))
            bukvi++;
        else if (isdigit(str[i]))
            cifri++;
        else
            spec++;
    }
    return (bukvi > 0 && cifri > 0 && spec > 0);
}


int main() {
    int MAX = 100;
    char s[MAX];
    cin.getline(s, MAX);

    cout << s << " ";
    if (e_validna_lozinka(s))
        cout << "e validna lozinka.";
    else
        cout << "NE e validna lozinka.";
    return 0;
}

2.8. Problem 7

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]))
                str[j] = toupper(str[i]);
            else if (isupper(str[i]))
                str[j] = tolower(str[i]);
            j++;
        }
        i++;
    }
    str[j] = '\0';
}

int main() {
    int MAX = 100;
    char s[MAX];
    cin.getline(s, MAX);

    filter(s);
    cout << s << endl;
    return 0;
}

2.9. Problem 8

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++) {
        s[i] = s[i + spaces];
    }
    s[i] = '\0';
    while (i > 0 && isspace(s[i - 1])) {
        i--;
        s[i] = '\0';
    }
}

int main() {
    int MAX = 100;
    char s[MAX];
    cin.getline(s, MAX);
    cout << "[" << s << "] -> ";
    trim(s);
    cout << "[" << s << "]";
    return 0;
}