/* * strings.cpp * * Created on: 15.11.2012 * Author: trifon */ #include #include using namespace std; const int MAX = 100; int main1() { char s[] = "Hello, world!"; cout << s << endl; cout << strlen(s) << endl; cout << s[strlen(s)-1] << endl; char s2[15] = ""; // <-> { '\0' } <-> { 0 } char s3[15] = ""; // cin >> s2; cin.getline(s2, 15); cout << "->" << s2 << "<-" << endl; //cin >> s3; cin.getline(s3, 15); cout << "->" << s3 << "<-" << endl; return 0; } int main2() { char a[5] = { 'H', 'e', 'l', 'l', 'o' }; char s[20] = "Hello, world!"; strcpy(s, "Goodbye, world!"); cout << s << endl; char s2[20] = ""; strcpy(s2, "Hello, world!"); // s2 = s; cout << s2 << endl; //!!! cout << strcpy(s, ";lkdfj;fjjadsfjladsfjkldfjdsjfkldfj") << endl; // cout << s2 << endl; cout << strcmp(s, s2) << endl; // strcpy(s2, s); if (strcmp(s, s2) == 0) { cout << "s == s2!" << endl; } if (strcmp(s, s2) != 0) { cout << "s != s2!" << endl; } cout << strcat(s, "?!") << endl; // !!!! strcat("abc", "def"); // !!! "e" != 'e' cout << strchr("abcdefg", 'e') << endl; cout << strstr("abcdefg", "bcd") << endl; cout << strlen(a) << endl; cout << a << endl; strncpy(s, "Hello, world!", 7); s[7] = '\0'; strncat(s, " I am here!", 5); cout << s << endl; cout << strncmp("Hello, nice world!", "Hello, cruel world!", 8); cout << endl; } int main3() { char s[MAX] = ""; cout << "Въведете низ на латиница: "; cin.getline(s, MAX); int n = strlen(s); int i = 0; while (i < n / 2 && s[i] == s[n-1-i]) i++; // i == n / 2 || s[i] != s[n-1-i] cout << "Низът \"" << s << "\""; if (i < n / 2) cout << " НЕ"; cout << " е палиндром" << endl; } int main4() { char s[MAX] = ""; cout << "Въведете низ: "; cin.getline(s, MAX); // думите се разделят с ' ', ',', '.', '!', '?' int n = strlen(s); int count = 0; bool word = false; for(int i = 0; i < n; i++) { bool mark = s[i] == ' ' || s[i] == ',' || s[i] == '.'; mark = mark || s[i] == '!' || s[i] == '?'; if (word && mark) word = false; if (!word && !mark) { word = true; count++; } } cout << "Низът съдържа " << count << " думи" << endl; } int main() { // 1+3+5/7-4*2 char s[MAX]; cin.getline(s, MAX); int result = 0;char op = '+'; for(int i = 0; s[i] != '\0'; i++) { if (s[i] >= '0' && s[i] <= '9') { int d = s[i] - '0'; switch (op) { case '+':result += d;break; case '-':result -= d;break; case '*':result *= d;break; case '/':result /= d;break; } } else op = s[i]; } cout << "=" << result << endl; }