I solve the problem, I do not understand how to make a replacement only in the first half of the line.

Task:
In the given line in the first half of the line, all points should be replaced with the ~ character.

Made only a replacement:

 #include <iostream> int main() { char stroka[] = "....."; std::cout << stroka << "\n"; for(int i = 0; stroka[i] != '\0'; ++i) { if(stroka[i] == '.') stroka[i] = '~'; } std::cout << stroka << "\n"; return 0; } 
  • 3
    Well, what's the problem to iterate only to half a line? - Andrej Levkovitch
  • one
    Instead of stroka[i] != '\0' make i < длина строки . Then divide the length by 2 and the problem is solved. - HolyBlackCat

4 answers 4

Let's go for a start, "the difficult way." Very complicated.

 #include <iostream> int main() { char stroka[] = "....."; std::cout << stroka << "\n"; int len = sizeof(stroka); for(int i = 0; i < len/2; ++i) { if(stroka[i] == '.') stroka[i] = '~'; } std::cout << stroka << "\n"; return 0; } 

But only with ++! Therefore, at least so.

 #include <iostream> int main() { std::string stroka = "....."; std::cout << stroka << "\n"; int len = stroka.length(); for(int i = 0; i < len/2; ++i) { if(stroka[i] == '.') stroka[i] = '~'; } std::cout << stroka << "\n"; return 0; } 

True, you need to think about what a half line is if the number of characters is odd.

  • one
    But only with ++!, We can solve it in one line .. - AR Hovsepyan
  • one
    Well, no need to say that your solution has one line. 3-4 lines per one (depending on how you count). And I think that your solution, although good, is hardly suitable for solving a school assignment. - KoVadim
  • Well, once for a school assignment, then there was no need to show the solution with std :: string, and if you use it, then you need to use its services, for example, as I will now add to the answer - AR Hovsepyan
  • one
    I think that std :: string is the most correct thing for a school assignment (unless the teacher tells you to use C in s ++) - KoVadim
  • I don't know what they teach in schools. When I was in school, then there was no personal computers, and in ordinary schools did not pass BT. And in such schools as mine (Fiz.mat), they only explained what bits and bytes are, bit operations, and Nairi 2’s computer occupied a whole class with its dimensions. So I don’t know if STL schools are currently learning containers? ... - AR Hovsepyan

Option without first calculating the length of the b-string:

 #include <iostream> void replace_before_middle(char * begin) { char const * last = begin; while (*last && *++last) { if (*begin == '.') { *begin = '~'; } ++begin; ++last; } } int main() { char str[] = ".+..."; replace_before_middle(str); std::cout << str << std::endl; } 
  • Offer readers to experience the joy of discovery? (with a coherent list (for example, by dividing it in half), it is at first more stunning) - avp
  • @avp did not understand what it is for. The code is not complicated, trite one pointer "jumps" two times faster than the other. - Croessmah
  • And by the number of plus signs you will be able to assess the ingenuity of the spherical middle horse ... - avp
 #include <algorithm> //... std::replace(stroka, stroka + strlen(stroka)/2, '.', '~'); 

// option two

 std::string stroka(".........."); size_t hf = stroka.length() / 2; cout << stroka.replace(0, hf, hf, '~' ); 
  • In the second variant, you replace any characters (and not just dots) in the half line with ~ . Write it clearly in response - avp
  • @avp, you are right, but I explicitly wrote that there are only std :: string stroka ("..........") in the line; So there are no other options and the condition of whether the character is a period, will always give the truth - AR Hovsepyan

I will offer my option:

 #include <iostream> int main() { char stroka[] = "....."; std::cout << stroka << "\n"; stroka[0] = '~'; stroka[1] = '~'; std::cout << stroka << "\n"; return 0; }