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.
Related
I was trying to remove X from a given string, the code compiles and runs but there is no output shown. I guess the problem is where I have to use the 'cout' operator.
Here is my Code:
#include<iostream>
#include<bits/stdc++.h>
#include<string.h>
using namespace std;
void removeX(char str[])
{
if(str[0]='\0')
{
cout<<str;
}
if(str[0]!='x'||str[0]!='X')
{
removeX(str+1);
}
else
{
for(int i=0;i!='\0';i++)
{
str[i]=str[i+1];
}
removeX(str+1);
}
}
int main()
{
char a[]="MALCOLM X";
removeX(a);
return 0;
}
You have a few issues in your code, the ones I can spot right away are:
if(str[0]='\0') - this is an assignment, not a comparison. Your entire string will be replaced with \0-characters - no characters will be skipped because this:
if(str[0]!='x'||str[0]!='X') is always true. Ask yourself if x is different from x (false) OR X (true). False or true = true.
The check should be implemented something like (str[0] != 'x' && str[0] != 'X').
Edit: One more issue.
The overall logic will not work (-ish). The part where you run through the string and compress it is correct enough. But you try to run to the end and then print the string, however at that point you are only holding the end of the string (the null termination), so no matter what else you have done you will only print that (aka. print nothing).
Instead, once you are done compressing the xs out of the string, you need to return to the beginning of the string and then print that. The easy way is to print in main, or you can split your function into an outer+inner function like:
void outer_removeX(str) {
removeX(str);
print(str);
}
Or you could add an extra variable to the recursion that allows you to return to the first call in the chain and then print there.
However, only printing the end will not work.
try the following code:
#include<iostream>
#include<bits/stdc++.h>
#include<string.h>
using namespace std;
void removeX(char str[],int j)
{
if(str[j]=='\0')
{
cout<<str;
}
else if(str[j]!='x' && str[j]!='X')
{
removeX(str,++j);
}
else
{
for(int i=j;str[i]!='\0';i++)
{
str[i]=str[i+1];
}
removeX(str,j);
}
}
int main()
{
char a[]="MAxLCOLM X";
removeX(a,0);
return 0;
}
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;
}
I am making a Program which will convert a string using recursion.
The method replaces all occurrences of upper case characters with "1" all occurrences of "o" with 2 and all occurrences of "r" with a.
Requirement: The Recursive function should take only one Parameter that is the input string and returns the converted string.
Below is my code:
#include<iostream>
#include<conio.h>
#include<string>
#include<string.h>
using namespace std;
int count=0;
string convert(string a)
{
int b=a.length();
if(b>=count)
{
if (isupper(a[b-(b-count)]))
{
a.replace(b-(b-count),1,"1");
convert(a);
}
else if (a[b-(b-count)]=='o')
{
a.replace(b-(b-count),1,"2");
convert(a);
}
else if(a[b-(b-count)]=='r')
{
a.replace(b-(b-count),1,"a");
convert(a);
}
}
else
return a;
}
void main()
{
string a;
a="ABCrroo";
int l=a.length();
cout<<convert(a);
getch();
}
You've defined convert() as returning a std::string but there are four places inside the function where you make a recursive call and do nothing with the return value. You have to do something if you want to pass the result of each recursive call back up the call stack.
I modified your code and changed the four bare convert(a); lines to return convert(a); and it outputs "212a212a212a" which I think is the desired result.
It also looks like you are using an index one past the end of the string. The condition if(b>=count) would allow your code to use an index outside the valid range of 0..length-1. Try changing it to just if(b>count).
#include<iostream>
#include<string>
#include<string.h>
string convert1(string a, int pos);
using namespace std;
int count=0;
string convert1(string a, int pos)
{
if(pos < a.length())
{
if(isupper(a[pos])) a.replace(pos, 1, "1");
else if(a[pos]=='o') a.replace(pos, 1, "2");
else if(a[pos]=='r') a.replace(pos, 1, "a");
convert1(a, ++pos);
}
else return a;
}
int main()
{
string a="oYoroCoroSor";
cout << a << endl;
int l=a.length();
cout<< convert1(a, 0) << endl;
return 0;
}
I can't figure out how to do this.
The question: Implement the function
int count_matches(const string arr[], int size, string query);
Return the number of strings in the array that are equal to query or
-1 if size if less than 0.
My answer:
#include <iostream>
#include <cmath>
#include<string>
using namespace std;
int count_matches(const string arr[], int size, string query){
int i = 0;
int numMatches;
for (;i<size;i++) {
if (string[i] == (string[i]+1)){
numMatches++;
}
}
return numMatches;
}
int main(){
string selection;
const string array[4]={"dog", "cat", "dog", "dog"};
cout<<"which animal do you want?"<<endl;
cin>> selection;
cout<< "there are " << count_matches(array, 4, selection)<< " matches"<<endl;
return 0;
}
What is wrong?
int count_matches(const string arr[], int size, string query){
int numMatches(0);
for (int i=0; i<size; ++i)
{
if (arr[i] == string)
{
++numMatches;
}
}
return numMatches;
}
This should be a solution to your problem but you will never learn anything from gaining the answer this way. I'd advise reading this tutorial on arrays and loops to better understand how to tackle the problem next time.
Once you get your code to compile, you need to look carefully at this block:
if (string[i] == (string[i]+1)) {
numMatches++;
}
What do you actually want to compare here? string is a type. Take another look at the arguments that you pass into the function and that should make it clear.
Then, once you have a match, you increment numMatches. But what value does this have to start with? What value should it have to start with?
Note that std::count() does exactly what your count_matches() function should do:
std::count(array, array + 4, selection)
This will probably not statisfy whomever marks your homework, but it is worth playing with and learning if you want to advance your C++ skills.
Good luck!
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.