I have one more question, I want to add a _ in front of every Capital letter which will be converted to lowercase, plus the first letter cannot be capital!! I cant figure out how to do it... :{ example:
input: loLollL, output: lo_loll_l
and I want it to go backwards too: input: lo_loll_l output: loLollL
code is here:
#include <iostream>
#include <algorithm>
using namespace std;
int main ()
{
const int max = 100;
string slovo;
int pocet_r;
cout << "Zadaj pocet uloh:" << endl;
cin >> pocet_r;
if(pocet_r >= 1 && pocet_r <=100)
{
// funkcia na zabezpecenie minimalneho poctu chars
for (int i = 0; i <pocet_r; i++)
{
cout << "Uloha " << i+1 << ":" << endl;
cin >> slovo;
if(slovo.size() > max)
{
cout << "slovo musi mat minimalne 1 a maximalne 100 znakov" << endl;
}
while( slovo.size() > max)
{
cin >> slovo;
}
for (int i=0; i <= slovo.size(); i++)
{
int s = slovo[i];
while (s > 'A' && s <= 'Z')
{
if(s<='Z' && s>='A'){
return s-('Z'-'_z');
}else{
cout << "chyba";
}
}
}
cout << slovo[i] << endl;
}
}else{
cout << "Minimalne 1 a maximalne 100 uloh" << endl;
}
system("pause");
}
EDIT>
for (int i=0; i <= slovo.size(); i++)
{
while (slovo[i] >= 'A' && slovo[i] <= 'Z')
{
string s = transform(slovo[i]);
cout << s << endl;
s = untransform(s);
cout << s << endl;
}
}
This should work:
#include <string>
#include <cctype>
#include <iostream>
using namespace std;
string
transform(const string& s)
{
const size_t n = s.size();
string t;
for (size_t i = 0; i < n; ++i)
{
const char c = s[i];
if (isupper(c))
{
t.push_back('_');
}
t.push_back(tolower(c));
}
return t;
}
string
untransform(const string& s)
{
string t;
const size_t n = s.size();
size_t i = 0;
while (i < n)
{
char c = s[i++];
if (c != '_')
{
t.push_back(c);
continue;
}
c = s[i++];
t.push_back(toupper(c));
}
return t;
}
int
main()
{
string s = transform("loLollL");
cout << s << endl;
s = untransform(s);
cout << s << endl;
}
Related
I want to write a program that adds words to a lexicon with pointers. However, I must not use strdup(), how can I do that? Below is what I tried but it gives me this error:
Exception thrown at 0x7C87EE72 (ucrtbased.dll) in (name of the file.exe): 0xC0000005: Access violation writing location 0xCDCDCDCD.
The problem is in the newStr() function.
#include <cstring>
#include <string>
#pragma warning (disable:4996)
#include <iostream>
using namespace std;
void delStr(char**&, int&, char*);
void newStr(char**&, int&, char*);
void printchar(char**, int, char);
char* searchStr(char**&, int&, char*);
void printAll(char**&, int);
enum ACTIONS { NEW, DELETE, SEARCH, PRINTLETTER, PRINTALL, EXIT };
int main() {
char** lexicon = NULL;//pointer to pointer fo the dictionary
int x = 0;
int size = 0;
char word[81];
char ch;
cout << "Enter 0-5:" << endl;
cin >> x;
while (x < 0 || x > 5) {//while loop for correct input
cout << "ERROR" << endl;
cin >> x;
}
while (x >= 0 && x <= 5) {
switch (x) {
case NEW:
cout << "Enter the word:" << endl;
ch = cin.get();
cin.getline(word, 80);
newStr(lexicon, size, word);
printAll(lexicon, size);
break;
case DELETE:
cout << "Enter the word to delete:" << endl;
ch = cin.get();
cin.getline(word, 80);
delStr(lexicon, size, word);
printAll(lexicon, size);
cout << endl;
break;
case SEARCH:
cout << "Enter the word to search for:" << endl;
ch = cin.get();
cin.getline(word, 80);
searchStr(lexicon, size, word);
break;
case PRINTLETTER:
cout << "Enter the char:" << endl;
ch = cin.get();
ch = cin.get();
printchar(lexicon, size, ch);
cout << endl;
break;
case PRINTALL:
if (size > 0 && lexicon != NULL) {
printAll(lexicon, size);
}
break;
case EXIT:
return 0;
break;
}
cout << "Enter your choice:" << endl;
cin >> x;
while (x < 0 || x > 5) {//while loop for correct input
cout << "ERROR" << endl;
cin >> x;
}
}
return 0;
}
void printAll(char**& lex, int size) {
for (int i = 0; i < size; i++) {
cout << lex[i] << " ";
}
cout << endl;
}
void newStr(char**& lex, int& size, char* word) {
int i = 0;
for (i = 0; i < size; i++) {
if (strcmp(lex[i], word) == 0) {
//cout << "Word " << word << " already exists\n";
return;
}
}
if (size == 0) {
lex = new char* [1];
for (int i = 0; i < strlen(word); i++) {
strcpy(*lex, word);
}
size++;
}
else {
char** temp = new char* [size + 1];
for (i = 0; i < size; i++) {
temp[i] = lex[i];
}
//strcpy(temp[size],word);
strcpy(temp[size], word);
delete[] lex;
lex = temp;
size++;
}
}
void delStr(char**& lex, int& size, char* word) {
int j = 0;
for (int i = 0; i < size; i++) {
if (strcmp(lex[i], word) == 0) {
for (j = i; j < size - 1; j++) {
lex[j] = lex[j + 1];
}
size--;
char** temp = new char* [size];
for (j = 0; j < size; j++) {
temp[j] = lex[j];
}
delete[] lex;
lex = temp;
}
}
}
void printchar(char** lex, int size, char ch) {
for (int i = 0; i < size; i++) {
if (lex[i][0] == ch) {
cout << lex[i] << " ";
}
}
}
char* searchStr(char**& lex, int& size, char* word) {
for (int i = 0; i < size; i++) {
if (strcmp(lex[i], word) == 0) {
cout << "Found" << endl;
//cout << "Word " << word << " already exists\n";
return lex[i];
}
}
cout << "Not found";
return NULL;
}
I am trying to create an algorithm for bruteForce cracking MD5 hash.
My goal is to measure the time consumption when splitting into fibers for the processor and optionally graphics in compute clastr.
I got stuck in creating an algorithm.
The input should be a string. According to the number of string characters, I need to create the same number of forcycles.
Statically written for 3 digist, it looks like this:
#include <iostream>
#include <string>
#include "md5.h"
using namespace std;
int main()
{
string imput = "slv";
cout << "imput string: "<< imput << endl;
cout << "MD5 HASH: "<< wantedHash << endl;
do
{
cout << '\n' << "Enable BruteForce Craker";
} while (cin.get() != '\n');
string s;
for(int i=0; i != 256; i++)
{
for(int j=0; j != 256; j++)
{
for(int k=0; k != 256; k++)
{
string s = md5(string(1,(char)i) + string(1,(char)j) + string(1,(char)k));
serchCounter++;
if(s == wantedHash)
{
cout << "Find: " << string(1,(char)i) + string(1,(char)j) + string(1,(char)k) << endl;
cout << "Count TestedHash: " << serchCounter << endl;
return 0;
}
}
}
}
return 0;
}
My idea .. something like that ...
#include <iostream>
#include <string>
#include "md5.h"
using namespace std;
string imput = "s";
string wantedHash = md5(imput);
double serchCounter = 0;
int bruteForse(int longString, string s)
{
for(int i=0; i != 256; i++)
{
string s = md5(string(1,(char)i));
serchCounter++;
if(s == wantedHash)
{
cout << "Find: " << string(1,(char)i);
cout << "Count TestedHash: " << serchCounter << endl;
return 0;
}
}
if(longString > 1) bruteForse(--longString, s);
return 0;
}
int main()
{
cout << "imput string: "<< imput << endl;
cout << "MD5 HASH: "<< wantedHash << endl;
bruteForse(imput.length(),imput);
}
I would do:
bool increase(std::string& s)
{
for (auto rit = s.rbegin(); rit != s.rend(); ++rit) {
auto& c = s[i];
if (c == -1) {
c = 0;
continue;
} else if (c == 127) {
c = -128;
} else {
++c;
}
return true;
}
return false;
}
void bruteForce(std::size_t size, const string& wantedHash)
{
std::string s;
s.resize(size);
do {
if (md5(s) == wantedHash) {
cout << "Find: " << s << std::endl;
}
} while (increase(s));
}
#include <iostream>
#include <cstring>
using namespace std;
int test(char[], int);
void decal(char[], int n);
int main()
{
char a[10], b[10], c[10];
int valid;
do {
cout << "Insert first number (maximum 5 chars hexa):" << endl;
cin >> a;
valid = test(a, strlen(a));
if(!valid)
cout << "Error." << endl;
} while (!valid);
cout << "First number: " << a << endl;
decalez(a, strlen(a));
cout << "First number after insert: " << a << endl;
do {
cout << "Insert 2nd number (maximum 5 chars hexa):" << endl;
cin >> b;
valid = test(b, strlen(b));
if(!valid)
cout << "Error." << endl;
} while (!valid);
decalez(b, strlen(b));
cout << "2nd number after insert: " << b << endl;
add(a, b); // Calculating c
cout << "Result: " << c << endl;
return 0;
}
int test(char x[], int n)
{
if(n > 5)
return 0; // Too many numbers
for(int i = 0; i < n; i++)
{
if(x[i] <48 || (x[i] > 57 &&
x[i] < 65) || (x[i] > 70 && x[i] < 97) || x[i] >
102)
return 0;
}
return 1;
}
void decal(char x[], int n)
{
int i, nz;
x[5] = '\0';
nz = 5 - strlen(x);
if(nz > 0) {
for(i = 0; i < n; i++)
x[5 - i- 1] = x[n-i-1];
}
for(i = 0; i < nz; i++)
x[i] = '0';
}
I was given this school project to make a hexadecimal calculator. The teacher made us the following code. Mandatory
My problem is the void add part. How is it possible to add char?
I know there are easier ways to make a hexadecimal calculator, but we have to use that code.
So how can I write a sum something like 1cec+bec=28d8 in hexa?
Assuming the existence of these functions
// Convert hexadecimal string 'number' into an integer.
int fromHex(const char* number);
// Convert 'number' to hexadecimal in 'result', assuming that the result will fit.
void toHex(int number, char* result);
you can write add like this:
void add(const char* hex_a, const char* hex_b, char* hex_c)
{
int a = fromHex(hex_a);
int b = fromHex(hex_b);
toHex(a + b, hex_c);
}
Implementing the conversion functions left as an exercise.
Here is an example of how you can add hexadecimal numbers.
This simplistic code wraps around with 5-digit-overflow.
#include <iostream>
#include <exception>
#include <cstring>
using std::cout;
using std::endl;
struct BadDigit : public std::exception { };
int hexToDec( const char c ) {
if ( c >= '0' && c <= '9' ) {
return c -'0';
}
else if( c>= 'A' && c<='F') {
return c - 'A' + 10;
}
else if ( c >= 'a' && c<='f' ) {
return c - 'a' + 10;
}
else {
throw BadDigit();
}
}
char decToHex( int d ) {
if ( d >= 0 && d <= 9 ) {
return d + '0';
}
else if ( d >= 10 && d <= 15 ) {
return d - 10 + 'A';
}
else {
throw BadDigit();
}
}
void add( const char *a, const char *b, char *s )
{
int rem = 0;
for (int i = 4; i >= 0; i-- ) {
int ai = hexToDec(a[i]);
int bi = hexToDec(b[i]);
int ri = ai + bi + rem;
s[i] = decToHex( ri % 16 );
rem = ri / 16;
}
}
int main()
{
const char *hex1 = "effff";
const char *hex2 = "00001";
char result[7];
try {
add(hex1, hex2, result);
}
catch (std::exception& e) {
cout << "Exception: " << e.what() << endl;
return 1;
}
cout << hex1 << " + " << hex2 << " = " << result << endl;
return 0;
}
I am getting a OUTOFRANGE error with vector in c++ when using insert method. I don't know why this is happening but I was able to narrow down the problem to one line through debugging. Here is the full code.
//
#include <cstdio>
#include <iostream>
#include <vector>
#include <fstream>
#include <cassert>
#include <string>
using namespace std;
class suffixArray{
public: suffixArray(std:: string concatenated ){
vector<int> attempt1;
const int size = (int)concatenated.length();
int rank[7] = {};
char *suffixPointers[concatenated.length()];
int value[concatenated.length()];
for(int i =0; i <= size-1; i++){
suffixPointers[i] = &concatenated[i];
value[i] = (int)concatenated[i];
}
std::cout << "[";
for(int i = 0; i<= size-1; i++){
std::cout <<value[i] << " ";
}
std::cout << "]"<< std:: endl;
for(int i = 0; i<=size -1; i++){
if(i == 0){
rank[i] = i;
attempt1.push_back(i);
}
else if(value[i] > value[i-1]){
rank[i] = i;
attempt1.push_back(i);
}else{
int current =i;
int savedValue = value[i];
int prevSavedRank;
int indexcounter = i;
while(savedValue <= value[attempt1.at(indexcounter-1)] && indexcounter - 1 >= 0 ){
indexcounter--;
}
cout << indexcounter << endl;
attempt1.insert(attempt1.begin() + indexcounter ,i);
// while(savedValue <= value[rank[current-1]] && current-1 >= 0){
// prevSavedRank= rank[current-1];
// rank[current-1] = i;
// rank[current] = prevSavedRank;
// current--;
// }
}
}
int now;
for(int i = 0; i<= 3; i++){
now = attempt1[i];
std::cout << now << " ";
}
}
};
void read_file(string filename, string& contents, int& num_lines){
ifstream f;
f.open(filename.c_str());
string line;
contents = "";
num_lines = 0;
while(getline(f, line)){
contents.append(line.substr(0, line.length()));
num_lines++;
}
f.close();
}
int main(int argc, const char* argv[]) {
std:: string test = "BANANA$";
suffixArray testString (test);
string fn;
string contents;
int num_lines;
cout << "File 1:" << endl;
cin>> fn;
read_file(fn, contents, num_lines);
cout << "Read: " << fn << "\n";
cout << " * " << num_lines << " lines\n";
cout << " * " << contents.length() << " characters (excluding newlines)\n";
//cout <<" * " << contents << endl;
// char * contents_cstring = (char*)contents.c_str();
//for(int i =0; i< contents.length(); i++){
// assert(contents_cstring[i] == *(contents_cstring + 1));
// assert(contents_cstring[i] == contents.at(i));
//}
//assert(contents_cstring[contents.length()] == '\0');
return 0;
}
I have narrowed down the problem to be the problem to be from this line, but can not figure out why it is occurring, or how to fix it.
attempt1.insert(attempt1.begin() + indexcounter ,i);
Consider the first time the program reaches
int indexcounter = i;
while(savedValue <= value[attempt1.at(indexcounter-1)] && indexcounter - 1 >= 0){
indexcounter--;
}
i will be 1. indexcounter-1 will be 0. If the loop is entered,
int indexcounter = 1;
while(savedValue <= value[attempt1.at(0)] && 0 >= 0 ){
1--;
}
OK, so what happens the next time?
while(savedValue <= value[attempt1.at(-1)] && -1 >= 0 ){
0--;
}
value[attempt1.at(-1)] happens before -1 >= 0, s the trap to prevent -1 fails. Reverse the order of the tests.
while(indexcounter - 1 >= 0 && savedValue <= value[attempt1.at(indexcounter-1)])
Could be more bugs, but after that the program hangs and asks for a file that I don't have.
This question already has answers here:
Convert a String In C++ To Upper Case
(31 answers)
Closed 7 years ago.
I am writing a program that can count the number of times a word is found in a external file. The words that are to be searched for are also in an external file but I am able to retrieve those just fine. I realised that it will only update the value of count if the word exactly matches. So for example if I was searching for the word "School" and the word "school" was in the textfile I don't think the value of count would be changed. I also think that count wouldn't be changed if the word to be search for was "SCHOOL" and the word in the textfile was "school". So how do I edit my if statements so that for example the word "school" would match "SCHOOL" AND "School"?
This is my main function:
#include <iostream>
#include "ReadWords.h"
#include "Writer.h"
#include <cctype>
#include <string>
using namespace std;
int main() {
int x = 9;
int count = 0;
int count0;
int count1;
int count2;
int count3;
int count4;
int count5;
int count6;
int count7;
int count8;
int count9;
int scount;
const int size = 10;
string word_search[size];
string word;
cout << "Please enter a filename: " << flush;
char filename[30];
cin >> filename;
ReadWords reader(filename);
while (reader.isNextWord()){
count = count + 1;
reader.getNextWord();
}
cout << "There are: " << count << " words in the play" << endl;
cout << "Please enter the name of the file with the search words: " << flush;
char filename1[30];
cin >> filename1;
ReadWords reader1(filename1);
scount = 0;
while (reader1.isNextWord()) {
word_search[scount] = reader1.getNextWord();
++scount;
}
cout << "" << endl;
while (reader.isNextWord()) {
This is where I attempted to convert the input to upper case to see if the word matches the uppercase version of itself but this didn't work. Here I also need to check if the word matches itself if the first letter is capital?
if (reader.getNextWord() == word_search[0] || toupper(reader.getNextWord()) == word_search[0]) {
count0 = count0 + 1;
}
if (reader.getNextWord() == word_search[1]) {
count1 = count1 + 1;
}
if (reader.getNextWord() == word_search[2]) {
count2 = count2 + 1;
}
if (reader.getNextWord() == word_search[3]) {
count3 = count3 + 1;
}
if (reader.getNextWord() == word_search[4]) {
count4 = count4 + 1;
}
if (reader.getNextWord() == word_search[5]) {
count5 = count5 + 1;
}
if (reader.getNextWord() == word_search[6]) {
count6 = count6 + 1;
}
if (reader.getNextWord() == word_search[7]) {
count7 = count7 + 1;
}
if (reader.getNextWord() == word_search[8]) {
count8 = count8 + 1;
}
if (reader.getNextWord() == word_search[9]) {
count9 = count9 + 1;
}
}
cout << "Please enter the name of the file to write to: " << flush;
char filename2[30];
cin >> filename2;
Writer reader2(filename2);
cout << "File has been written too.." << endl;
reader2.writeInt(count);
reader2.writeString("Hello my name is Joshua Ogunnote");
return 0;
}
This is a separate file where some of my functions are declared:
#include "ReadWords.h"
#include <cstring>
#include <iostream>
using namespace std;
void ReadWords::close(){
wordfile.close();
}
ReadWords::ReadWords(const char *filename) {
wordfile.open(filename);
if (!wordfile) {
cout << "could not open " << filename << endl;
exit(1);
}
}
string ReadWords::getNextWord() {
string n;
if(isNextWord()){
wordfile >> n;
int len = n.length();
for(int i = 0; i < len ; i++) {
if (ispunct(n[i]))
{
n.erase(i--, 1);
len = n.length();
}
}
cout << n << endl;
return n;
}
}
bool ReadWords::isNextWord() {
if (wordfile.eof()) {
return false;
}
return true;
}
If you're just using English, a simple tolower() transform will do.
std::string tolower( std::string s )
{
for (char& c : s) c = std::tolower( c );
return s;
}
Now you can compare them:
if (tolower( "Hello" ) == tolower( "HELLO" ))
If you are working with Unicode, you should perform a conversion called case folding on the text, and compare the resulting string data.