I try to reach at least the function of reversing the line, but have not mastered the communication of functions with arrays
#include <iostream> #include <clocale> #include <cstring> using namespace std; char string_turnover(int n, char s[n]){ int i; char buff; for(i=0; i<(n/2); i++){ buff=s[i]; s[i]= s[ ni ]; s[ni]= buff; } return s; } int main() { setlocale(LC_CTYPE, "rus"); char user_string[500]; cout << "Введите вашу строку(латиницей): "; cin >> user_string; int length=strlen(user_string); cout << "Длинна строки равна " << length << endl; char short_string[length]; char revolutionary_string[length]= string_turnover(length, short_string); cout<< revolutionary_string; return 0; } what's wrong? qt complains that "n" and "s" are not declared in the function, but are also used for the string
char revolutionary_string[length]= string_turnover(length, short_string); writes
/home/petr-55vd/untitled/main.cpp:26: ошибка: array must be initialized with a brace-enclosed initializer char revolutionary_string[length]= string_turnover(length, short_string); ^ can try through the user (or better through the link). If it is, then how to do it (how to change the elements of the array then in the function in places)
UPDATE
made through dynamic memory allocation, but the last problem remains: the function does not accept a string giving out
ошибка: invalid conversion from 'char' to 'const char*' [-fpermissive] strcpy(revolutionary_string,string_reverse(length, user_string)); ^ and pointing to the line
strcpy(revolutionary_string,string_reverse(length, user_string)); the code itself looks like
char string_reverse(int n, char* s){ int i; char buff; for(i=0; i<(n/2); i++){ buff=s[i]; s[i]= s[ ni ]; s[ni]= buff; } return *s; } int main() { setlocale(LC_CTYPE, "rus"); //ввод пользовательской строки char user_string[500]; cout << "Введите вашу строку(латиницей): "; cin >> user_string; int length=strlen(user_string); cout << "Длина строки равна " << length << endl; char *revolutionary_string = new char[length]; strcpy(revolutionary_string,string_reverse(length, user_string)); cout << revolutionary_string << endl; return 0; }
-std=c99- 0andriy