Home > Back-end >  Delete the string specified position, why would have been wrong answer
Delete the string specified position, why would have been wrong answer

Time:10-30

https://zoj.pintia.cn/problem-sets/91827364500/problems/91827366485
Title address
Novice in asking big inspiration, thank you! (string into a char array can ac, don't understand why can't use string)

#include
#include
#include
#include
# define ll long long

using namespace std;

Void delchar (int pos, string & amp; S)
{
Int len=s.l ength ();
For (int I=pos. i{
S=s [I - 1] [I];


}
S [len - 1]='\ 0';
}

Int main ()
{
Int k, pos, CNT=1;
string s;
Cin> k;
While (k -)
{
Cin> Pos.
Cin> s;

Delchar (pos, s);
coutCnt++;

}



}

CodePudding user response:

Because your test environment is the C language, not a C + +, on the other hand, if the replacement into a char, then delchar function of s.l ength (); How to deal with?

CodePudding user response:

GCC is a C language compiler

CodePudding user response:

Didn't you watch the output is correct and meet the requirements of the subject? Output and proved by the requirement,
 
Int k, pos, CNT=1;
The string s, ss;
cin> k;
While (k -)
{
cin> Pos.
cin> s;
Delchar (pos, s);
Ss +=cnt++ + '0'.
Ss +=' ';
Ss +=s;
Ss +='\ n';
}
cout

CodePudding user response:

Two questions
1 the string header file is not a string starting with c cstrings corresponding c language string. H
2 string is not 0 0 at the end or not regulations end so s [len - 1]='\ 0'; Cannot be guaranteed to be effective
The final output to cout

CodePudding user response:

reference 1/f, confident boy reply:
test environment is a C language because of you, not C + +, on the other hand, if the replacement into char, then delchar function of s.l ength (); How to deal with?

As to strlen together, then can be

CodePudding user response:

reference 5 floor DesperateStrive reply:
Quote: reference 1/f, confident boy reply:
test environment is a C language because of you, not C + +, on the other hand, if the replacement into char, then delchar function of s.l ength (); How to deal with?

We together into strlen, then can

That is the problem, the compiler you are using C + + language, which is the C language compiler
  • Related