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; } 
  • one
    The length is written with one n. This is also a mistake. - HasmikGaryaka
  • then create a dynamic array? - Kurnyakov Peter
  • use std :: vector - tilin
  • one
    @tilin, you can, but only on the stack, the length may not be known in advance. -std=c99 - 0andriy
  • one
    @ 0andriy and although not. it was about c ++. And there such a standard is not provided. Although gcc and clang implement it. - tilin

3 answers 3

Yes, you violate the syntax of the language, that's all :)

Instead

 char string_turnover(int n, char s[n]){ 

write

 char string_turnover(int n, char *s){ 

Well, instead of

 char revolutionary_string[length]= string_turnover(length, short_string); 

since it is impossible to initialize an array with a value that is calculated at runtime,

 char revolutionary_string[length]; strcpy(revolutionary_string,string_turnover(length, short_string)); 

I hope there is enough space ...

Did not notice right away - so you have the length calculated? Then it is no good and

 char revolutionary_string[length]; 

use either a dynamic array, or, even better, the usual standard string — you can initialize it as many as you like :)

Update

I have just given a reversal function in another answer ...

 void reverse(char * b, char * e) { while(b < e) { char t = *b; *b++ = *e; *e-- = t; } } 

And call as reverse(s,s+strlen(s)-1) .

  • one
    I would also add the code s [ni] = buff; leads to memory corruption when i = 0. - Voidificator
  • string already has a ready-made reverse, but this is unlikely to be recounted. - HasmikGaryaka
  • Still, e must decrement if it is a reversal. - HasmikGaryaka
  • @HasmikGaryaka Yes, of course. I did not copy-paste, but I changed hands, so I was mistaken. There - ru.stackoverflow.com/a/729008/195342 - a little differently written. - Harry

My version

 #include <iostream> #include <clocale> #include <cstring> #include <conio.h> using namespace std; void string_turnover(int n, char *s){ int i; char buff; for(i=0; i<(n/2); i++){ buff=s[i]; s[i]= s[ ni-1 ]; s[ni-1]= buff; } } 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=new char[length]; strcpy(short_string,user_string); string_turnover(length, short_string); cout<< short_string; _getch(); return 0; } 
  • here the function returns nothing - Kurnyakov Peter
  • @ Kurnyakov Peter why return, if the passed parameter is reversed. - HasmikGaryaka
  • understood, thanks) - Kurnyakov Peter

Possible variant:

 char* string_turnover(int n, char* s) { for(int i = 0; i < n/2; ++i) std::swap(s[i], s[n-1-i]); return s; } 

Just remember that n must match the number of characters without a terminating zero.

  • task-coup without third-party libraries (vectors-sided) - Kurnyakov Peter
  • Where did you see my vector? And even if I used vectors, this is the standard library, not the third-party library. - Voidificator
  • swap-function of vectors cannot be used without connecting <vector> - Kurnyakov Peter
  • swap has nothing to do with vectors. She simply rearranges her arguments in places. If it is forbidden to use it, make a permutation with handles - especially in your code there is such a permutation. - Voidificator