বিষয়বস্তুতে চলুন

প্রোগ্রামিংয়ের মৌলিক ধারণা/সি++ স্ট্রিং উদাহরণ

উইকিবই থেকে

স্ট্রিং

[সম্পাদনা]
 #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;

}


আউটপুট

[সম্পাদনা]

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

// এই প্রোগ্রামটি একটি ফাইল তৈরি করে, ফাইলে ডেটা যোগ করে,
// ফাইলটি প্রদর্শন করে, ফাইলে আরও ডেটা অ্যাপেন্ড করে,
// পুনরায় প্রদর্শন করে এবং তারপরে ফাইলটি মুছে দেয়।
// ফাইল যদি আগে থেকেই থাকে, তাহলে এটি চালানো যাবে না।
//
// তথ্যসূত্র :
//     https://www.mathsisfun.com/temperature-conversion.html
//     https://en.wikibooks.org/wiki/C%2B%2B_Programming


#include 
#include 
#include 
#include 

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);

}


আউটপুট

[সম্পাদনা]

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

তথ্যসূত্র

[সম্পাদনা]

টেমপ্লেট:Subpage navbar