char Array in C++ is not functioning properly - c++

I am coding a C++ program using char array.But i think it is giving problems.
This is my code:
#include <iostream>
#include<string.h>
using namespace std;
int main() {
long int t;
cin>>t;
char total[500],a[500],b[500];
cin>>total;
int len=strlen(total);
//cout<<strlen(total);
for(int i=0;i<len/2;i++){
a[i]=total[i];
}
for(int i=0;i<len/2;i++){
b[i]=total[i];
}
cout<<a<<endl;
cout<<b;
return 0;
}
It is not printing the arrays. But when i commented out
/*for(int i=0;i<len/2;i++){
b[i]=total[i];
}*/
it is printing array a as expected. What is the problem here?

You arrays are not NULL-terminated. If I add a[len/2]=0; and b[len/2] = 0; after the for loops, it works correctly.

Related

Exercise to verify if a word == it's reverse

I've come into some problem with my code. At a first glance, it seems that I've done everything right and the code should work. But, it isn't. This exercise asks me to verify if a word is equal to it's reverse.
This is my code:
#include <iostream>
#include <cstring>
using namespace std;
int main(){
char cuv[255], cuv1[255]=""; cin>>cuv;
int j = strlen(cuv)-1;
for(int i = 0; i<strlen(cuv); i++)
cuv1[i]=cuv[j-i];
cuv1[strlen(cuv1)+1]='\0';
if(cuv==cuv1)
cout<<"cuvantul este palindrom";
else
cout<<"cuvantul nu este palindrom";
return 0;
}
This becomes much easier with std::string and std::equal:
std::string cuv = "ANNA";
return std::equal(std::begin(cuv), std::end(cuv), std::rbegin(cuv));
Or if for some reason you want to stick with C strings:
char cuv[255] = "ANNA";
int len = strlen(cuv);
return std::equal(cuv, cuv+len, std::reverse_iterator<char*>(cuv+len));
This method works fine, I've gave up trying to compare the words, so I compared the word's letters from start with those from the end.
#include <iostream>
#include <cstring>
using namespace std;
int main(){
char cuv[255]; cin>>cuv;
int n = strlen(cuv);
int k = 0;
for(int i = 0; i!=strlen(cuv)/2;i++)
if(cuv[i]!=cuv[n-i-1])
k=1;
if(k==0)
cout<<"cuv pal";
else
cout<<"cuv != pal";
return 0;
}
problem is with this statement
cuv1[strlen(cuv1)+1]='\0'
Change this to
cuv1[strlen(cuv1)]='\0';

scanf for both character and integer

I am trying to take input character and integer both. But when i use cin>>ch>>val; for taking input,it works.But Using scanf("%c%d",&ch,&val);,it shows me run time error.What can i do to get rid of this problem? I want to use scanf for faster input.
Here is my partial code:
#include<bits/stdc++.h>
using namespace std;
int main()
{
int q;
scanf("%d",&q);
while(q--)
{
char ch;
int val,in;
//cin>>ch>>val;
scanf("%c %d",&ch,&val);
in=val;
if(ch=='a'){
//scanf("%d",&val);
//update(1,0,m,++indx,val);
printf("First Case\n");
}else{
//si(in);
//if(in>tree[1]) printf("none\n");
//else query(1,0,m,in);
printf("Second Case\n");
}
}
}
I ran this code! It works fine for me. So probably you're using scanf in C++ and forgot to include or maybe it's something else with your code.
Can you share the full code. There is no issue with scanf() function as you have mentioned. You can take multiple inputs like scanf(%d%d%c%c,&a,&b,&c,&d);
#include <stdio.h>
int main()
{
printf("Hello World");
int q=1;
while(q--)
{
char ch;
int val,in;
scanf("%c%d",&ch,&val);
in=val;
if(ch=='a'){
printf("%d",val);
}else{
printf("%d",val);
}
}
return 0;
}

Character replacement not working

enter image description hereI have the following code that is supposed to replace instances of pi (3.14) with the word "pi". For example, "x3.14 3.14 3.14xx" should be turned into "xpi pi pixx". However, my code isn't doing that; why doesn't it work, and how would I fix it?
#include<iostream>
#include<cstring> //dsfsdf
#include<string>
#include<cstdio>//sdfdsf
using namespace std;
void replacepi(char *arr,int i)
{
//base
if(arr[i]=='\0')
{
cout<<arr<<endl;
return;
}
//recc
if(arr[i]=='3' && arr[i+1]=='.' && arr[i+2]=='1' && arr[i+3]=='4')
{
arr[i]='p';
arr[i+1]='i';
for(int j=i+4;arr[j]!='\0';j++)
arr[j-2]=arr[j];
arr[strlen(arr)-2]='\0';
}
replacepi(arr,i+1);
}
int main() {
long int n;
cin>>n;
for(int i=0;i<n;i++)
{
char arr[1000];
cin.getline(arr,sizeof(arr));
replacepi(arr,0);
}
return 0;
}
Looks like a job for regex_replace. Given you've read your input into arr you can just dump the replacement to the console like this:
regex_replace(ostream_iterator<char>(cout), cbegin(arr), cend(arr), regex("3\.14"), "pi")
Live Example
Naturally arr should be a string as in the example rather than a char[], as given any size char[] the input may be larger, a string will always work.

conversion string to character in c++ Not Working Properly

make a function which receive the file name but it not working properly because it receives "Doctor.txtG" but I am giving "Doctor.txt" how can i resolve it?My code is Given below......
#include <iostream>
#include <conio.h>
#include <fstream>
using namespace std;
int number_of_lines = 0;
int numberoflines(string A);
int main()
{
cout<<numberoflines("Doctor.txt");
getch();
return 0;
}
int numberoflines(string A)
{
int Len;
char Chr[Len];
Len=A.length();
A.copy(Chr, Len);
//cout<<Len;
cout<<Chr;
string line;
ifstream myfile(Chr);
if(myfile.is_open())
{
while(!myfile.eof())
{
getline(myfile,line);
number_of_lines++;
}
myfile.close();
}
return number_of_lines;
}
It needs to copy a null-terminated byte into Chr.
Use
strcpy(Chr, A.c_str());
instead of A.copy(Chr, Len);
And you should properly init Chr like
char Chr[1024]
or
char* Chr = new char[Len + 1].
Your problem is happening because you are trying to create a char array with the size Len. But you have not initialized Len before using it. This is why it is resulting in undefined behavior and creating this problem. Always try to initialize variables when you declare them. Otherwise, this problem will happen quite often.
However, You don't need to create another char array. Just use std::string::c_str(); in your parameter for the constructor of the ifstream. I am giving a sample code below. This should solve your problem.
#include <iostream>
#include <conio.h>
#include <fstream>
using namespace std;
int number_of_lines = 0;
int numberoflines(string A);
int main()
{
cout<<numberoflines("Doctor.txt");
getch();
return 0;
}
int numberoflines(string A)
{
string line;
ifstream myfile(A.c_str());
if(myfile.is_open())
{
while(!myfile.eof())
{
getline(myfile,line);
number_of_lines++;
}
myfile.close();
}
return number_of_lines;
}

Segmentation fault(SIGSEGV) in my code

Im getting Segmentation fault(SIGSEGV) for many problems that iv solved in spoj and other websites. Im giving the problm statement link and code. Can anybody tell me the mistakes that iv done below.
Problem :
http://goo.gl/CVROl
Thanks in advance.
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
int main()
{
char a;
string final,dummy;
int t,h,p,k,z;
scanf("%d",&t);
while(t)
{
cin>>a>>h>>p;
final=a;
for(int i=0;i<h;i++)
{
k=0;
for(int j=0;final[j]!='\0';j++)
{
if(final[j]=='a')
{
dummy[k]='a';
dummy[k+1]='b';
k+=2;
}
else
{
dummy[k]='b';
dummy[k+1]='a';
k+=2;
}
}
final=dummy;
}
printf("%c\n",final[p-1]);
t--;
}
return 0;
}
As pointed out by sstn, you did not allocate memory for dummy. Since it's a string and it looks like you just want to append characters to it, you can do:
for(int j=0;j < final.size();j++)
{
if(final[j]=='a')
{
dummy.push_back('a');
dummy.push_back('b');
}
else
{
dummy.push_back('b');
dummy.push_back('a');
}
}
final=dummy;
final is of type std::string, which aren't null terminated.
In your for loop: for(int j=0;final[j]!='\0';j++) you're checking for the end of the string as you would do in C (which a char* -- null terminated string), but in C++ you should iterate over string characters in some other ways: using an iterator, counting up the string size or something.
A fast quick to this problem (don't know whether there are others) is to get the null terminated char* representation of final: final.c_str() and iterater over it as you're doing.