C++ 如何在字符串中搜索字符?

C++ 如何在字符串中搜索字符?,c++,C++,我正在编写一个程序,将短日期(mm/dd/yyyy)转换为长日期(2014年3月12日),并打印出日期 在以下用户输入的情况下,程序必须工作: 10/23/2014 9/25/2014 12/8/2015 2016年1月1日 我让程序处理第一个用户输入,但我不确定如何处理字符串第一个位置没有“0”的用户输入 #include <iostream> #include <string> using namespace std; int main() { stri

我正在编写一个程序,将短日期(mm/dd/yyyy)转换为长日期(2014年3月12日),并打印出日期

在以下用户输入的情况下,程序必须工作: 10/23/2014 9/25/2014 12/8/2015 2016年1月1日

我让程序处理第一个用户输入,但我不确定如何处理字符串第一个位置没有“0”的用户输入

#include <iostream>
#include <string>

using namespace std;

int main() 
{
    string date; 
    cout << "Enter a date (mm/dd/yyyy): " << endl;
    getline(cin, date);

    string month, day, year;

    // Extract month, day, and year from date
    month = date.substr(0, 2);
    day = date.substr(3, 2);
    year = date.substr(6, 4);

    // Check what month it is 
    if (month == "01") {
        month = "January";
    }
    else if (month == "02") {
        month = "February";
    } 
    else if (month == "03") {
        month = "March";
    } 
    else if (month == "04") {
        month = "April";
    } 
    else if (month == "05") {
        month = "May";
    } 
    else if (month == "06") {
        month = "June";
    } 
    else if (month == "07") {
        month = "July";
    } 
    else if (month == "08") {
        month = "August";
    } 
    else if (month == "09") {
        month = "September";
    } 
    else if (month == "10") {
        month = "October";
    } 
    else if (month == "11") {
        month = "November";
    } 
    else {
        month = "December";
    }

    // Print the date
    cout << month << " " << day << "," << year << endl;
    return 0;
}
#包括
#包括
使用名称空间std;
int main()
{
字符串日期;

正如红蛇在评论中写道的那样:使用搜索
/
,例如

#包括
int main()
{
std::string date=“09/28/1983”;
int startIndex=0;
int endIndex=date.find('/');
std::string month=date.substr(startIndex,endIndex);
startIndex=endIndex+1;
endIndex=date.find(“/”,endIndex+1);
std::string day=date.substr(startIndex,endIndex-startIndex);
std::string year=date.substr(endIndex+1,4);

std::cout您还可以利用流转换,获得效率较低但更简单的解决方案:

#include <iostream>
#include <string>
using namespace std;

int main() {

    string months[] = {"", "January", "February", "Mars", "April", "May", "June", "Jully", "August", "September", "October", "December"};

    cout << "Enter a date (mm/dd/yyyy): " << endl;
    char c;
    int day, month, year;
    cin >> day >> c >> month >> c >> year;
    // error handling is left as an exercice to the reader.
    cout << months[month] << " " << day << ", " << year << endl;
    return 0;
}
#包括
#包括
使用名称空间std;
int main(){
字符串月份[]={”、“一月”、“二月”、“火星”、“四月”、“五月”、“六月”、“七月”、“八月”、“九月”、“十月”、“十二月”};
日期>>c>>月份>>c>>年份;
//错误处理留给读者作为练习。

cout,而不是在预先确定的索引处获取子字符串。查找
/
索引并从那里剪切。我想使用find member函数…类似于date.find(“/”,0)从第0个位置查找/字符和搜索。是否应该在日期中解析月份、日期和年份?@ JIMT将给出在您的<代码>日期、子字符串< /代码>中使用的偏移量,而不是固定值。此外,还应考虑字符串转换为整数以支持“01”和“1”。请使用index = STOI(月)和if-like-if(index==1)month=“一月”的更改条件当前程序支持'01'和'1',而不更改index=stoi(month)中的代码。在if语句中,我使用if(month==“1”)
#include <iostream>
#include <string>
using namespace std;

int main() {

    string months[] = {"", "January", "February", "Mars", "April", "May", "June", "Jully", "August", "September", "October", "December"};

    cout << "Enter a date (mm/dd/yyyy): " << endl;
    char c;
    int day, month, year;
    cin >> day >> c >> month >> c >> year;
    // error handling is left as an exercice to the reader.
    cout << months[month] << " " << day << ", " << year << endl;
    return 0;
}