wanted to read only nums from file & store them in array.i don't know file handling. please help me out.
#include iostream conio.h
#stdio.h string fstream;
using namespace std;
int main()
{
ifstream myfile("ashishdata.txt");//file open
char c;
char arr[100];
int i=0;
// read data from file
while (myfile.get(c))
{
if(isdigit(c) || c=='.' )
{
arr[i] = c;
i++;
}
}
arr[i]='\0';//mark end of array
myfile.close();//file closed
int k;
k=strlen(arr);
//parsing string
int newarr[100];
int m=0;
string(s);
for(i=0;i<=k;i++)
{
s.erase();
while(arr[i] != '.')
{
s+=arr[i];
i++;
}
newarr[m]=atoi(s.c_str());
m++;
}
newarr[m]='\0';
//printing newarray
i=0;
while(newarr[i]!='\0')
{
cout<<newarr[i]<<'\t';
i++;
}
cout<<endl;
getch();
return(0);
}
Try this:
filename.txt:
4545.454
sds
65.65.6.65656
#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
using namespace std;
int main() {
char c;
char arr[100];
size_t i = 0;
// read data from file
ifstream myfile;
myfile.open("filename.txt", ios::in);
while (!myfile.eof()) {
myfile >> c;
if (isdigit(c) || c == '.') {
arr[i] = c;
i++;
}
}
arr[i] = '\0'; // mark end of array
myfile.close(); // file closed
size_t k = strlen(arr);
// parsing string
int newarr[100];
int m = 0;
string s;
for (i = 0; i <= k; i++) {
s.erase();
while (arr[i] != '.') {
s += arr[i++];
}
newarr[m++] = atoi(s.c_str());
}
newarr[m] = '\0';
// printing newarray
i = 0;
while (newarr[i] != '\0') {
cout << newarr[i++] << '\t';
}
return 0;
}
Related
I am trying to erase particular characters using the erase() function from a string but its not working.
The question says you have to remove a substring which is either "AB" or "BB". When the substring gets deleted from the string the remaining parts of the string get concatenated and the process continues...
Here is the code:
#include <bits/stdc++.h>
#define ios ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define int long long int
using namespace std;
int32_t main()
{
ios;
int t;
cin>>t;
while(t--)
{
string s;
cin>>s;
int i;
for(i=0;i<s.length();i++)
{
if(s[i]=='A'&&s[i+1]=='B')
{
s.erase(i,i+1);
cout<<s<<"\n";
i=-1; // I want to start from begining therefore initializing i=-1 after i++ it becomes i=0;
}
else if(s[i]=='B'&&s[i+1]=='B')
{
s.erase(i,i+1);
cout<<s<<"\n";
i=-1;
}
}
//cout<<s.length()<<"\n";
}
return 0;
}
Input:
1
AABBBABBBB
The output is :
ABBABBBB
BBABBBB
BABBBB
BBBB
BBB
BB
B
But the output should be:
ABBABBBB
BABBBB
BBBB
BB
Am I doing something wrong?
You can change two s.erase(i, i + 1); statements to s.erase(i, 2);.
1
AABBBABBBB
ABBABBBB
BABBBB
BBBB
BB
And you can use C++ STL.
#include <vector>
#include <iostream>
#include <string>
int main() {
int t{0};
std::cin >> t;
while (t--) {
std::string s{};
std::cin >> s;
for (auto i = 0; i < s.length() && i + 1 < s.length(); i++) {
if (s[i] == 'A' && s[i + 1] == 'B') {
s.erase(i, 2);
std::cout << s << std::endl;
i = -1;
} else if (s[i] == 'B' && s[i + 1] == 'B') {
s.erase(i, 2);
std::cout << s << std::endl;
i = -1;
}
}
}
return EXIT_SUCCESS;
}
Am I doing something wrong?
Yes, almost everything.
#include <iostream>
#include <string>
#include <regex>
int main()
{
std::regex re("AB|BB");
int t;
std::cin >> t;
while(t--)
{
std::string s;
std::cin >> s;
std::string prev;
// do one replacement at a time, until there are no changes
do
{
std::cout << s << '\n';
prev = s;
s = std::regex_replace(s, re, "", std::regex_constants::format_first_only);
} while (s != prev);
}
return 0;
}
It's just a simple variation of bubble sort.
#include <iostream>
#include <string>
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin >> t;
while(t > 0 && t--)
{
string s;
cin >> s;
for(size_t i = 0; i < s.length(); i++) {
for(size_t j = 0; j < s.length() - 1; j++) {
if((s[j]=='A' || s[j] == 'B') && s[j+1]=='B')
{
s.erase(j,2);
cout<<s<<"\n";
break;
}
}
}
}
return 0;
}
I need to create a function that counts chars in a string in C++, for example, when reading in from a text file that contains:
"aaabbbbbggssj"
the output should be:
"3a5b2g1j"
Im really not sure how to go about it and any help would be greatly appreciated! Thanks
My code is as follows:
#include <iostream>
using namespace std;
#include "Character.h"
#include <fstream>
#include <string>
using namespace std;
int main(){
int c;
void count(char [], int []);
//opening text file
string line = " ";
fstream my_stream;
my_stream.open("information.txt");
//loop to cout occurences of each character
while (getline (my_stream,line))
{
cout << line << '\n';
c += line.length();
cout << c;
int numOfChars = line.length();
for (unsigned int i = 0; i < line.length(); i++){
}
my_stream.close();
return 0;
}
Just count repeating symbols and produce result:
string compress(string const& str) {
string result = "";
int i = 0;
while (str[i]) {
int c = 1;
while (str[i] == str[i + c]) ++c;
result += std::to_string(c) + str[i];
i += c;
}
return result;
}
I am working on a brute force task, but when I run my program, it gives an empty output file. Can anybody please help me fix this? The problem statement is below along with my code after that.
PROBLEM STATEMENT:
Write a program that reads two numbers (expressed in base 10):
N (1 <= N <= 15)
S (0 < S < 10000)
and then finds and prints (in base 10) the first N numbers strictly greater than S that are palindromic when written in two or more number bases (2 <= base <= 10).
Solutions to this problem do not require manipulating integers larger than the standard 32 bits.
CODE
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <cmath>
#include <fstream>
using namespace std;
string convert(int num, int base)
{
int quo = 100000;
int rem = 0;
string to_reverse;
while (quo > 0)
{
quo = num / base;
rem = num % base;
to_reverse += to_string(rem);
num /= base;
}
reverse(to_reverse.begin(), to_reverse.end());
return to_reverse;
}
bool is_pal(string conv_num)
{
string reversed_conv_num = conv_num;
reverse(reversed_conv_num.begin(), reversed_conv_num.end());
if (reversed_conv_num == conv_num)
{
return true;
}
return false;
}
int main()
{
ofstream fout("dualpal.out");
ifstream fin("dualpal.in");
int n, start;
fin >> n >> start;
vector<int> finals;
int times = 0;
for (int i = start + 1; i <= 10000; i++)
{
if (times == n)
{
for (auto x : finals)
{
fout << x << "\n";
}
break;
}
else
{
for (int j = 2; j <= 10; j++)
{
if(is_pal(convert(i, j)) == true)
{
times++;
finals.push_back(i);
}
}
}
}
return 0;
}
Try this code. I made some changes.
#include <iostream>
#include <sstream>
#include <string>
#include <algorithm>
#include <vector>
#include <cmath>
#include <fstream>
using namespace std;
string convert(int num, int base)
{
int quo = 100000;
int rem = 0;
string to_reverse;
ostringstream str1; /*this and the next commented lines are added because "to_string" didnt work in my compiler*/
while (quo > 0)
{
quo = num / base;
rem = num % base;
str1 << rem; //this
to_reverse += str1.str(); //and this
num /= base;
}
reverse(to_reverse.begin(), to_reverse.end());
return to_reverse;
}
bool is_pal(string conv_num)
{
string reversed_conv_num = conv_num;
reverse(reversed_conv_num.begin(), reversed_conv_num.end());
if (reversed_conv_num == conv_num)
{
return true;
}
return false;
}
int main()
{
ofstream fout;
fout.open("dualpal.out", ios::out); //open the file in binary mode
//ifstream fin("dualpal.in");
int n, start;
cin >> n >> start;
vector<int> finals;
int times = 0;
for (int i = start + 1; i <= 10000; i++)
{
if (times == n)
{
//just a simple iterator for vector
for (vector<int>::iterator it = finals.begin(); it != finals.end(); ++it)
{
fout << *it << "\n";
}
break;
}
else
{
for (int j = 2; j <= 10; j++)
{
if(is_pal(convert(i, j)) == true)
{
times++;
finals.push_back(i);
}
}
}
}
fout.close(); //close the file
return 0;
}
USE OF STRING STREAMS
int number = 1000;
ostringstream s;
s << number;
string str = s.str();
This method can be used to convert number to strings.
This code requires <sstream> header file.
I cannot figure out why this function won't work.
I want to return a string what is trimmed from the char array passed into the function at the index where it finds an identifying char. Is there something obvious I'm missing? As it is, this only returns the first letter of the input char[]...
#include <iostream>
#include <cstring>
using namespace std;
string trim(char in[], char token){
char A[300];
for(int i = 0; i < strlen(in); i++){
if(in[i] != token){
A[i] = in[i];
} else
A[i] = '\0';
break;
}
return A;
}
int main()
{ char statement[] = {"weight of car is ?1 ton"};
cout << trim(statement, '?') << endl;
return 0;
}
Because you break; in the first iteration. Use a block to avoid it.
#include <iostream>
#include <cstring>
using namespace std;
string trim(char in[], char token){
char A[300];
for(int i = 0; i < strlen(in); i++){
if(in[i] != token){
A[i] = in[i];
} else {
A[i] = '\0';
break;
}
}
return A;
}
int main()
{ char statement[] = {"weight of car is ?1 ton"};
cout << trim(statement, '?') << endl;
return 0;
}
Note that calling strlen too many times isn't a good idea.
This should be better:
#include <iostream>
using namespace std;
string trim(char in[], char token){
char A[300];
bool token_found = false;
for(int i = 0; in[i] != '\0'; i++){
if(in[i] != token){
A[i] = in[i];
} else {
A[i] = '\0';
token_found = true;
break;
}
}
if (token_found) {
return A;
} else {
return in;
}
}
int main()
{ char statement[] = {"weight of car is ?1 ton"};
cout << trim(statement, '?') << endl;
return 0;
}
Examples:
char test1[] = " ";
char test2[] = " hello z";
char test3[] = "hello world ";
char test4[] = "x y z ";
Results:
" "
" olleh z"
"olleh dlrow "
"x y z "
The problem:
Reverse every world in a string, ignore the spaces.
The following is my code. The basic idea is to scan the string, when
finding a word, then reverse it. The complexity of the algorithm is
O(n), where n is the length of the string.
How do verify it? Is there a better solution?
void reverse_word(char* s, char* e)
{
while (s < e)
{
char tmp = *s;
*s = *e;
*e = tmp;
++s;
--e;
}
}
char* word_start_index(char* p)
{
while ((*p != '\0') && (*p == ' '))
{
++p;
}
if (*p == '\0')
return NULL;
else
return p;
}
char* word_end_index(char* p)
{
while ((*p != '\0') && (*p != ' '))
{
++p;
}
return p-1;
}
void reverse_string(char* s)
{
char* s_w = NULL;
char* e_w = NULL;
char* runner = s;
while (*runner != '\0')
{
char* cur_word_s = word_start_index(runner);
if (cur_word_s == NULL)
break;
char* cur_word_e = word_end_index(cur_word_s);
reverse_word(cur_word_s, cur_word_e);
runner = cur_word_e+1;
}
}
Your code seems correct, but it's plain C. In C++, using the same approach, it could look something like this:
#include <iostream>
#include <string>
#include <iterator>
#include <algorithm>
#include <cctype>
int main()
{
std::string str = " cat cow dog wolf lobster";
std::string result;
auto it = str.begin();
while (it != str.end()) {
while (it != str.end() && isspace(*it)) {
result += *it;
++it;
}
auto begin = it;
while (it != str.end() && !isspace(*it)) {
++it;
}
auto end = it;
result.append(std::reverse_iterator<decltype(end)>(end),
std::reverse_iterator<decltype(begin)>(begin));
// if you want to modify original string instead, just do this:
std::reverse(begin, end);
}
std::cout << result <<'\n';
}
In-place, ANSI C89.
#include <ctype.h>
#include <stdio.h>
void reverse(char *s) {
int i = 0, j, k;
while (1) {
while (isspace(s[i])) i++;
if (!s[i]) return;
for (j = i; !isspace(s[j]) && s[j] != '\0'; j++);
for (k = 0; k < (j - i) / 2; k++) {
char t = s[i + k];
s[i + k] = s[j - k - 1];
s[j - k - 1] = t;
}
i = j;
}
}
int main(int argc, char**argv) {
if (argc != 2) return 1;
reverse(argv[1]);
printf("%s\n", argv[1]);
return 0;
}
#include<iostream>
#include<string.h>
using namespace std;
int main()
{
char s[89];
cout << "enter\n";
gets(s);
int k;
int p = strlen(s);
strcat(s," ");
for(int i=0; i <= p; i++)
{
if(s[i]==' ')
{
for (k = i-1; (k != -1) && (s[k] != ' '); k--)
cout<<s[k];
cout<<" ";
}
}
return 0;
}
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
char str[100],a[10],s[100]=" ";
int i,j;
cout<<"enter a string";
cin.getline(str,100);
strcat(s,str);
for(i=0;s[i]!='\0';i++)
{
if(s[i]==' '&&s[i+1]!=' '&&s[i+1]!='\0')
{
cout<<" ";
if(i==0)cout<<"\b";
j=i+1;
while(s[j]!=' '&&s[j]!='\0')
{
j++;
}
j--;
while(s[j]!=' ')
{
cout<<s[j];
j--;
}
}
else if(s[i]==' ')
{
cout<<" ";
if(i==0)cout<<"\b";
}
}
return 0;
}
#include <iostream>
#include<string.h>
#include<stdio.h>
using namespace std;
void Reverse_Each(char st[60])
{
int i,j,k=0,p=0,l;
char sr[60];
l=strlen(st);
for(i=0;i<=l;i++) {
if((st[i]==' ')||(st[i]=='\0')) {
for(j=i-1;j>=p;j--) {
sr[k]=st[j]; k++;
}
sr[k]=' '; k++; p=i+1;}
}
for(i=0;i<p;i++)
cout<<sr[i];
}
int main(){
char s[60];
gets(s);
Reverse_Each(s);
return 0;
}