How is this possible that a char type array is acting like string type data type

char S[N];

int main()

{

scanf("%d", &q);

for (; q; q --)

{

scanf("%s", &S[1]);

n = strlen(S + 1);

// i found this on codechef editorial please help because of this i m not able to get AC what is the concept

Please open this link in this setters solution please have look you will get to know that instead of string data type char array is used which is acting like a string

#include
#include<bits/stdc++.h>
#include
#include
using namespace std;
int A[2][1001];
char s[1001];
int main()
{
int t;
cin>>t;

while(t--)
{
    int N;
    
 cin>>s;
 
    
    N=strlen(s);
 
    
    for(int i=1; i<=N; i++)
    {
        for(int j=0; j<=1; j++)
        {
            A[j][i]=A[j][i-1]+(s[i]==j + '0');
        }
    }
    int mx=N;
    for(int w=0; w<=1; w++)
    
        for(int i=1; i<=N; i++)
        
            for(int j=i; j<=N; j++)
            
                mx=min(mx,A[w][i-1]+ (j-i+1) - (A[w][j]-A[w][i-1]) +(A[w][N]-A[w][j]));
                 cout<<mx<<endl;       
   
    memset(A,0,sizeof(A));
    memset(s,0,sizeof(s));
}
return 0;

}

//the imediate above is my code this is not running just one sample case if it runs i may get AC please help @rishabhmahajan546

@AyushKumar9032 it is because character arrays are designed to act like strings. Actually string datatype came later, earlier all operations were performed with the help of a char array. Do watch all the videos in the sections of character arrays and strings and everything will become clear.

I know that string came later but my problem is that I want to use as I have been using normal every one does

char s[1001];

cin>>s; it should store all the zeros and ones as per the question but what Is in editorial is

char s[1001];

scanf("%s",&s); this difference in the two code is not letting me get the test case solved I haved solved this codechef question after about 3 months @rishabhmahajan546 bhaiya please help

@AyushKumar9032hey bhai,ye cin jo hai C++ me use hota hai and scanf C me use hota hai ,dono input lene ke kamm ate hai ,ap koi bhi use kr skte ho ,scanf C++ me bhi chl jata hai ,koi bhi use krlo no difference.

@AyushKumar9032 aap simpy char array ko bhi cin se le skte hai ,like string ko lete hai.

but dono code mae koi to difference hai
char s[1001]; // this one is from editorial
scanf("%s",&s);

char s[1001]; // this one is mine
cin>>s;

https://discuss.codechef.com/t/prfyit-editorial/46620 //codechef editorial link

answer accept ni hora ha apne wle input se

#include
#include<bits/stdc++.h>
#include
#include
using namespace std;
int A[2][1001];
char s[1001];
int main()
{
int t;
cin>>t;

while(t--)
{
    int N;
    
 cin>>s;
 
    
    N=strlen(s);
 
    
    for(int i=1; i<=N; i++)
    {
        for(int j=0; j<=1; j++)
        {
            A[j][i]=A[j][i-1]+(s[i]==j + '0');
        }
    }
    int mx=N;
    for(int w=0; w<=1; w++)
    
        for(int i=1; i<=N; i++)
        
            for(int j=i; j<=N; j++)
            
                mx=min(mx,A[w][i-1]+ (j-i+1) - (A[w][j]-A[w][i-1]) +(A[w][N]-A[w][j]));
                 cout<<mx<<endl;       
   
    memset(A,0,sizeof(A));
    memset(s,0,sizeof(s));
}
return 0;

}

yeh mera code hai ek barr ap ise sample test cases ke lie run kreke please dekhiye
mere code se o/p ata hai
my code o/p editorial code o/p
1 2
3 3
0 0
0 0

bs input lene le alag trike se alg o/p arraha hai

I am seeing this video from 3 months lol now i got it basically it uses prefix sum
noo i made it myself settters solution mae ek code mae choti si problem hai please try to resolve that

this my code link https://ide.codingblocks.com/s/220237

this one is editorial https://discuss.codechef.com/t/prfyit-editorial/46620

difference dono code mae bs itna hai ki … i took input like this
char s[1001];
int main()
{
int t;
cin>>t;

    while(t--)
    {
        int N;
        
     cin>>s;
   int n=strlen(s);

//and editorial is like this

char S[N];

int main()

{
scanf("%d", &q);
for (; q; q --)
{
scanf("%s", &S[1]);
n = strlen(S + 1);

@rishabhmahajan546 if you understood my problem then please reply bhaiya

@AyushKumar9032 bhai cin shayad islie work nhi krrha kyunki cin me space ke bad input ko consider nhi kia jata whereas scanf me leleta hai ,ek bar aap getline(cin,s) use krke dekho ,ki chl rhe hai ki nhi.

is question input is trah lena hai 101010110 to space to lena hi nhin hai i have tried using cin.get() and cin.ignore after taking input for test case because uske baad string input lena hain without spaces

@AyushKumar9032 bhai it may be a reason ki printf fast hota hai in comparison to scanf ,see this article:
https://www.geeksforgeeks.org/fast-io-for-competitive-programming/amp/

but output differ nhin krna chiye na
correct o/p is
2
3
0
0
and my output is
1
3
0
0
that website says either use scanf printf
OR
use ios_base::sync_with_stdio(false);
cin.tie(NULL);
i included below one but o/p different aata hai

@AyushKumar9032 let me check ,I will tell you after running the code .

@AyushKumar9032 scanf is just faster than cin, but if there are no spaces in the string them both of then should work the same. It is possible that there is some other logical error in your code. Please save your code on ide.codingblocks.com and share the link for future reference.

https://ide.codingblocks.com/s/220237 please resolve my issue