Untitled

 avatar
unknown
plain_text
3 years ago
5.4 kB
10
Indexable
//8.01

bool check(string s) {
	for (int i = 0; i < s.size(); i++) {
		if (s[i] >= 48 && s[i] <= 57) {
			return false;			
			break;
		}
	}
	return true;
}

int main() {
	string s;
	getline(cin, s);
	check(s);
	if(check(s) == true) cout << s;
	else cout << "CHUOI KHONG HOP LE.";
}

//8.02

#include <iostream>
#include <string.h>
#include <cstring>
using namespace std;
#define MAX 300

int myStrlen(char s[]) {
    int a = 1;
    for (int i = 0; i < MAX; i++) {
        if (s[i] == '\n') s[i] = '\0';
        if (s[i] == '\0') return a;
        a++;
    }
}
void deleteS(char s[], int x, int& n) {
    for (int i = x; i < n - 1; i++) {
        s[i] = s[i + 1];
    }
}
void insert(char s[], int j, int& n, char x) {
    for (int i = n - 1; i >= j; i--) {
        s[i + 1] = s[i];
    }
    s[j] = x;
    n++;
}
void Chuanhoa(char s[]) {
    int n = myStrlen(s);
    char temp[MAX]{};
    int i = 0;
    int k = 0;
    while (i < n) {
        if (s[i] == ' ' && s[i + 1] == '.') {
            deleteS(s, i, n);
            continue;
        }
        if (s[i] == ' ' && s[i + 1] == ' ') {
            deleteS(s, i, n);
            continue;
        }
        if (s[i] == '.' && s[i + 1] == ' ' && s[i + 2] == ' ') {
            deleteS(s, i + 1, n);
            continue;
        }
        if (s[i] == '.' && s[i + 1] != ' ') {
            insert(s, i + 1, n, ' ');
        }
        i++;
    }
}
int main()
{
    char s[MAX];
    cin.getline(s, MAX);
    cout << s << endl;
    Chuanhoa(s);
    cout << s;
    return 0;
}

//8.03

#include <iostream>
#include <cstring>
using namespace std;

char* ChuanHoa(char* s) {
    int i, j, n;
    n = strlen(s);
    while (s[0] == ' ') {
        memmove(s, s + 1, n);
        n = n - 1;
    }
    s[0] = toupper(s[0]);
    while (s[n - 1] == ' ') {
        s[n - 1] = 0;
        n = n - 1;
    }
    for (i = 0; i < n; i++) {
        j = i;
        while (s[j] == ' ') {
            if (s[j + 1] == ' ') {
                for (int k = j; k < n; k++)    s[k] = s[k + 1];
                n = n - 1;
            }
            else j++;
        }
    }
    s[n] = 0;
    for (i = 0; i < n; i++) {
        if (i == 0 || s[i - 1] == ' ') s[i] = toupper(s[i]);
        else s[i] = tolower(s[i]);
    }

    return s;
}

int main() {
    char hoten[100];
    cin.getline(hoten, 100);
    char* a = ChuanHoa(hoten);
    cout << a;
}

//8.05

#include <iostream>
#include <cstring>
using namespace std;

void count(char s[100][20], char size) {
    int count = 0, count2 = 0;
    for (int i = 0; i < size; i++) {
        for (int j = i; j < size; j++) {
            if (strcmp(s[i], s[j]) == 0)
                count++;
        }
        for (int k = 0; k < i; k++) {
            if (strcmp(s[i], s[k]) == 0) {
                count2 = 1;
                break;
            }
        }
        if (count2 == 0)
            cout << s[i] << ": " << count << endl;
        count = 0;
        count2 = 0;
    }
}
void Split(char s[]) {
    char temp[100][20];
    int i = 0;
    char* p;
    for (p = strtok(s, " "); p != NULL; p = strtok(NULL, " ")) {
        strcpy(temp[i], p);
        i++;
    }
    count(temp, i);
}


int main() {
    char a[1000];
    fflush(stdin);
    cin.getline(a, 999);
    if (strlen(a) == 0) cout << "Chuoi rong.";
    else
        Split(a);
    return 0;
}

//8.06 

#include <iostream>
#include <string>
#include <string.h>
using namespace std;

void reverse(string s) {
	string s1;
	for (int i = 0; i < s.length(); i++) {
		if (s[i] != ' ') s1 = s[i] + s1;
		else {
			cout << s1 << " ";
			s1.clear();
		}
	}
	cout << s1;
}

int main() {
	string s;
	getline(cin, s);
	if (s.length() == 0) cout << "Chuoi rong.";
	else reverse(s);
    return 0;
}

//8.07

#include <iostream>
#include <cstring>
using namespace std;

void string_reverse(char str[]) {
    int i, j, len;
    char ch;
    j = len = strlen(str) - 1;
    i = 0;
    while (i < j) {
        ch = str[j];
        str[j] = str[i];
        str[i] = ch;
        i++;
        j--;
    }
}

int main() {
    char s1[1000];
    cin.getline(s1, 999);
    char reverse[1000] = "";
    char temp[50];
    int i, j, n;
    n = strlen(s1);
    if (n == 0) cout << "Chuoi rong.";
    else {
        for (i = n - 1; i >= 0; --i) {
            for (j = 0; i >= 0 && s1[i] != ' '; --i, ++j)
                temp[j] = s1[i];
            temp[j] = '\0';
            string_reverse(temp);
            strcat(reverse, temp);
            strcat(reverse, " ");
        }
        cout << reverse;
    }
    
    return 0;
}

//8.08

#include <iostream>
#include <string>
using namespace std;


int main() {
	string s, s1;
	getline(cin, s);
	getline(cin, s1);
	int k;
	cin >> k;
	if (k < 0 || k > s.size()) cout << "Vi tri " << k << " khong thoa dieu kien.";
	else {
		s.insert(k, s1);
		cout << s;
	}

}

//8.09

#include <iostream>
#include <cstring>
#include <string.h>
using namespace std;

int main() {
    char s1[255], s2[255];
    cin.getline(s1, 255);
    cin.getline(s2, 255);
    int k = strcmp(s1, s2);
    if (k < 0) cout << "-1";
    else if (k == 0) cout << "0";
    else cout << "1";
    return 0;
}

//8.10

#include <iostream>
#include <string>
#include <string.h>
using namespace std;

int main() {
	string s, s1, s2;
	getline(cin, s1);
	getline(cin, s2);
	if (s1.size() + s2.size() > 255) cout << "Khong noi duoc. Khong du bo nho.";
	else cout << s1 + s2;
}