Programming Fundamentals/String Examples C++

From Wikibooks, open books for an open world
Jump to navigation Jump to search

Strings[edit]

#include <algorithm>
#include <iostream>
#include <string>

using namespace std;

string toLower(string);
string toUpper(string);

int main() {
    string str = "Hello";

    cout << "string: " << str << endl;
    cout << "tolower: " << toLower(str) << endl;
    cout << "toupper: " << toUpper(str) << endl;
    cout << "string.find('e'): " << str.find('e') << endl;
    cout << "string.length(): " << str.length() << endl;
    cout << "string.replace(0, 1, \"j\"): " << str.replace(0, 1, "j") << endl;
    cout << "string.substr(2, 2): " << str.substr(2, 2) << endl;

    string name = "Bob";
    double value = 123.456;
    cout << name << " earned $" << fixed << setprecision (2) << value << endl;

}

string toLower(string str) {
    transform(str.begin(), str.end(), str.begin(), ::tolower);

    return str;
}

string toUpper(string str) {
    transform(str.begin(), str.end(), str.begin(), ::toupper);

    return str;
}

Output[edit]

string: Hello
tolower: hello
toupper: HELLO
string.find('e'): 1
string.length(): 5
string.replace(0, 1, "j"): jello
string.substr(2, 2): ll
Bob earned $123.46

Files[edit]

// This program creates a file, adds data to the file, displays the file,
// appends more data to the file, displays the file, and then deletes the file.
// It will not run if the file already exists.
//
// References:
//     https://www.mathsisfun.com/temperature-conversion.html
//     https://en.wikibooks.org/wiki/C%2B%2B_Programming

#include <fstream>
#include <iomanip>
#include <iostream>
#include <string>

using namespace std;

double calculateFahrenheit(double celsius);
void createFile(string);
void readFile(string);
void appendFile(string);
void deleteFile(string);
int fileExists(string);

int main() {
    string FILENAME = "~file.txt";

    if(fileExists(FILENAME)) {
        cout << "File already exists." << endl;
    } else {
        createFile(FILENAME);
        readFile(FILENAME);
        appendFile(FILENAME);
        readFile(FILENAME);
        deleteFile(FILENAME);
    }
}

double calculateFahrenheit(double celsius) {
    double fahrenheit;

    fahrenheit = celsius * 9 / 5 + 32;
    return fahrenheit;
}

void createFile(string filename) {
    fstream file;
    float celsius;
    float fahrenheit;
    
    file.open(filename, fstream::out);
    if (file.is_open()) {
        file << "Celsius,Fahrenheit\n";
        for(celsius = 0; celsius <= 50; celsius++) {
            fahrenheit = calculateFahrenheit(celsius);
            file << fixed << setprecision (1) << celsius << "," << fahrenheit << endl;
        }
        file.close();
    } else {
        cout << "Error creating " << filename << endl;
    }
}

void readFile(string filename)
{
    fstream file;
    string line;

    file.open(filename, fstream::in);
    if (file.is_open()) {
        while (getline(file, line))
        {
            cout << line << endl;
        }
        file.close();
        cout << endl;
    } else {
        cout << "Error reading " << filename << endl;
    }
}

void appendFile(string filename)
{
    fstream file;
    float celsius;
    float fahrenheit;
    
    file.open(filename, fstream::out | fstream::app);
    if (file.is_open()) {
        for(celsius = 51; celsius <= 100; celsius++) {
            fahrenheit = calculateFahrenheit(celsius);
            file << fixed << setprecision (1) << celsius << "," << fahrenheit << endl;
        }
        file.close();
    } else {
        cout << "Error appending to " << filename << endl;
    }
}

void deleteFile(string filename)
{
    remove(filename.c_str());
}

int fileExists(string filename)
{
    FILE *file;
    
    file = fopen (filename.c_str(), "r");
    if (file != NULL)
    {
        fclose (file);
    }
    return (file != NULL);
}

Output[edit]

Celsius,Fahrenheit
0.0,32.0
1.0,33.8
2.0,35.6
...
98.0,208.4
99.0,210.2
100.0,212.0

References[edit]