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;
}
Related
so i have an input.txt file that contains english-german words
for example book-buch, area-Bereich etc (those "-" are also included)
i have to code a small translator so if input book the code has to output buch and vise versa
i already coded the english to german output but for some reason when i try the same logic on german to english it doesnt output anything
#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
#include <math.h>
using namespace std;
int main() {
ifstream ifs("input.txt");
ofstream ofs("history.txt", ifstream::app);
int a, b, c = 0, j;
string m, l, p[100];
for (a = 0; a < 50; a++) {
ifs >> p[a];
m = p[a];
}
cin >> l;
for (a = 0; a < 50; a++) {
if (l == p[a].substr(l.size() + 1, p[a].size())) { //here's the problem
cout << p[a].substr(0, p[a].size() - l.size() + 1);
}
if (l == p[a].substr(0, l.size())) { //english to german
cout << p[a].substr(l.size() + 1, p[a].size());
ofs << l << "-" << p[a].substr(l.size() + 1, p[a].size());
ofs << endl;
}
}
i used a different method
for (a = 0; a < 50; a++) {
pos = p[a].find("-");
if (l == p[a].substr(pos + 1)) {
pos = p[a].find("-");
cout << p[a].substr(0, pos);
also somehow forgot that c++ compiler was case sensitive
silly me
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.
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;
}
I'm a novice programmer just trying out some exercise on C++. In this case the exercise was to reverse a string. However, when I tried to reverse a string, on some occasions the output had some weird characters at the back. Could anyone tell me what's wrong? I've tried looking through the code thoroughly and can't figure out what's wrong.
Example:
Input : Hello
Output: olleH0]
�k�]�y��jv�#�y�
#include <iostream>
#include <string.h>
using namespace std;
string FirstReverse(string str) {
int k,y,z;
k = strlen(str.c_str());
y = k-1;
char str2[k];
for (z = 0; z < k; z++) {
str2[z] = str.c_str()[y];
if (y==0) break;
else y--;
}
return str2;
}
int main() {
string x;
getline(cin, x);
cout << FirstReverse(x);
return 0;
}
EDIT:
Turns out I could do it without using C styled strings by simply:
int k,y,z;
k = str.length();
y = k-1;
string str2;
for (z = 0; z < k; z++) {
str2 += str[y];
y--;
}
return str2;
algorithm includes a function called reverse
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
string my_string = "Hello World";
reverse(my_string.begin(), my_string.end());
cout << my_string << endl;
return 0;
}
I'm trying to build a program able to read all the characters including control characters and whitespaces in a jpg file .... i made it read \n or \t but i can't add there both together or any other control character ...
here's my code..
#include <string>
#include <bitset>
#include <iostream>
#include<fstream>
using namespace std;
int main(){
ifstream file("E:\\2.jpg", ios::binary);
string myString;
ofstream fout("E:\\mnmn.txt");
while(getline(file,myString,'\n') )
{
for (size_t i = 0; i < myString.size(); ++i)
{
fout <<"."<< bitset<8>(myString.c_str()[i]);
}}
return 0;
}
Anyone can help ?
Change
while(getline(file,myString,'\n') )
{
for (size_t i = 0; i < myString.size(); ++i)
{
fout <<"."<< bitset<8>(myString.c_str()[i]);
}
}
to
int c;
while( (c = file.get()) != EOF )
{
fout <<"."<< bitset<8>(c);
}