In a given string, change the given substring by another substring, no matter how many times it appears in the string. Help me please. Here is the code:

#include "stdafx.h" #include <stdio.h> #include <stdlib.h> #include <string.h> int main() { int i = 0, r; char str[51], tmp[26] = {'\0'}, k; printf("Vvedite stroku: "); scanf("%s",&str); printf("\n"); r=strlen(str); strncpy(tmp, str, r / 2); i = 0; while(str[i]) str[i++] = str[i + r / 2]; strcat(str, tmp); printf("Otvet: %s\n",str); return 0; } 

Closed due to the fact that off-topic participants fori1ton , VenZell , AntonioK , null , DeKaNszn May 8, '15 at 9:05 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • "Questions asking for help with debugging (" why does this code not work? ") Should include the desired behavior , a specific problem or error, and the minimum code for playing it right in the question . Questions without an explicit description of the problem are useless for other visitors. See How to create minimal, complete, repeatable example . " - fori1ton, VenZell, AntonioK, null, DeKaNszn
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • @ABIT, Explain what exactly you can not. Give an example of your current code. - Nicolas Chabanovsky

2 answers 2

What you have written clearly does not correspond to the task.

You need to enter 3 lines. The first, the one in which we are looking for and changing. The second is what we are looking for. Third - what we change.

Determine, also, the fourth, the one in which the result will be obtained.

Note that scanf ("% s", str) (str is correct , not & str (but you can & str [0]) (figure it out yourself why )) introduces a sequence of characters up to the first space, tab, or similar separator (more man). Is that exactly what you want? You can use fgets (str, sizeof (str), stdin) in your case to enter the full line.

To search for substrings in a string, it is convenient to use the function strstr (). To solve this problem, it must be called in a loop, each time shifting the beginning of the search in the source line. The cycle is completed, of course, when no entry is found.

With the details (copying), I hope, figure it out.

Please note that if it is necessary to replace the original line, it is not always possible if the result length exceeds the size of the original line.

  • I demand the use of std :: string !!! - gecube
  • one
    I did) #include "stdafx.h" #include <iostream> #include <conio.h> using namespace std; int main () {char a [100], b [1], c [1]; cout << "BBeDiTe TEKST" << '\ n'; cin.getline (a, 100); cout << "Symbol kotoruy nyzno zamenit" << '\ n'; cin >> b; cout << "\ n Symbol kotorum zamenit" << '\ n'; cin >> c; for (int i = 0; i <100; i ++) {if (a [i] == b [0]) a [i] = c [0]; } cout << "\ n Resultat:" << a; _getch (); } - ABIT
  • Well done ! Since one character is a special case of a substring, the first step has been taken. - avp

C ++ console:

 #include <vcl.h> #include <iostream> #include <string.h> #pragma hdrstop using namespace std; #pragma argsused 

void rcout (char * string) // for displaying Cyrillic console { char new_string [255]; CharToOem (string, new_string); cout << new_string; }

int main(void) { string s, s1, s2; m: rcout("Введите исходную строку: "); getline(cin, s); rcout("Введите искомое слово: "); getline(cin, s1); string::size_type npos, s1_len = s1.length(), s2_len = s2.length(); if ( s.find(s1) == std::string::npos ) //если искомое слово не найдено { rcout("Искомое слово не найдено!"); cout<<"\n"; goto m; } else //если найдено { rcout("Введите заменяющее слово: "); getline(cin, s2); while(npos != string::npos) { npos = s.find(s1); s.replace(npos, s1_len, s2); npos = s.find(s1, npos + s2_len); } rcout("Результат: "); cout<< s <<"\n"<< endl; goto m; } return 0; }

  • Plus. Using standard containers and algorithms is correct. - gecube
  • one
    @gecube, why don't you gossip for goto? - avp
  • In general, yes, in this case it was easier to use while (1) {...} and the break / continue constructs. - gecube