| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1548 人关注过本帖
标题:新手求教strlwr()
只看楼主 加入收藏
阿瞳正传
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2017-3-21
结帖率:0
收藏
已结贴  问题点数:20 回复次数:5 
新手求教strlwr()
这个可以直接用在字符串比较中吗?下面是一个输入学生姓名查学号的小程序。然后有一个功能是输入学生姓名(英文名),无论大小写都可以。所以我在构造姓名字符串的时候都用小写,然后在isValidName()里面用strlwr()把输入的字符串都变成小写(红字的部分),再一一比较。这样可行吗?其他部分都没有问题了,就红字部分。求各位大神帮帮忙

//源码
#include<iostream>
#include<cmath>
#include <string>   
#include <string.h> #include<iostream>
#include<cmath>
#include <string>   
#include <string.h> //将大写字母转换成小写字母用strlwr
#include<ctime>   
using namespace std;
string sarr[] = {"carl", "elle", "keith", "prad", "molly", "bunmi", "kim" };   
bool isValidName(string pname);  
string converNumber(int x);
string convertName(string pname);

int main(){
    string pname;
    int arr[10];
    int i = 0;
    int answer;
    int correctAnswer = 0;
    int incorrectAnswer = 0;
    char m;
    cout << "Please enter your name:";
    cin >> pname;
    cout << endl;

    if (isValidName(pname) == true){
        cout << "C-number" << ":" << convertName(pname) << " starting the test..." << endl;
        do{
            unsigned seed = time(0);     
            srand(seed);         
            int a = rand() % 10 + 1;   
            int b = rand() % 10 + 1;
            cout << pname << ",what is the remainder after " << converNumber(a) << " is divided by " << converNumber(b) << endl;
            cin >> answer;
            if (answer == a%b)
            {
                cout << "Well down "<<pname<<" you are correct." << endl;
                correctAnswer++;
            }
            else
            {
                cout << "That's not right,"<<pname<<" the correct answer is " << a%b << endl;
                incorrectAnswer++;
            }
            cout << "Do you want to continue(y/n):";
            cin >> m;


        } while (m == 'y' || m == 'Y');  
        cout << "Correct answer:" << correctAnswer << endl;
        cout << "Incorrect answer:" << incorrectAnswer << endl;
        cout << "Percentage answered correctly:" << (double)correctAnswer / (correctAnswer + incorrectAnswer)*100 << "%" << endl;
    }
    else{
        cout << pname << ",you are not required to take this test";
    }
    return 0;
}

bool isValidName(string pname){
    for (int i = 0; i<6; i++){
        if (strlwr(pname) == sarr[i]) //把pname变成小写字母再进行比较
            return true;
    }
    return false;
}

string convertName(string pname){
    string narr[7] = { "C531006","C531007", "C531010", "C532011", "C532015", "C531178", "C532196" };
    string result = "";
    for (int i = 0;i<7; i++){
        if (pname == sarr[i])
            result = narr[i];
    }
    return result;
}

string converNumber(int x){
    int num[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
    string str[] = { "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten" };
    string result = "";
    for (int i = 0; i < 10; i++){
        if (x == num[i])
            result = str[i];
    }
    return result;
}


[此贴子已经被作者于2017-3-21 11:22编辑过]

搜索更多相关主题的帖子: 姓名 字符串 英文名 include 
2017-03-21 11:20
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分:20 
这个可以直接用在字符串比较中吗?
听不懂问什么,strlwr是将字符串转化为小写,和“字符串比较”有什么关系?

strlwr(pname)
strlwr的原型是 char* strlwr( char* str ),你传一个 std::string 给它,当然编译失败啦
2017-03-21 12:33
阿瞳正传
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2017-3-21
收藏
得分:0 
回复 2楼 rjsp
那我如果想在输入pname的时候,比如里面构造的字符串有“carl”,希望无论输入“carl”、“CARL”、还是“Carl”都可以。那么我怎么改这个程序呢?谢谢


[此贴子已经被作者于2017-3-22 15:06编辑过]

2017-03-22 15:03
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分:0 
一定要用 strlwr 的话
程序代码:
#include <iostream>
#include <string>

int main( void )
{
    std::string s = "CarL";

    strlwr( &s[0] );

    if( s == "carl" )
        std::cout << "equal" << std::endl;
    else
        std::cout << "not equal" << std::endl;
}

2017-03-22 15:17
阿瞳正传
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2017-3-21
收藏
得分:0 
回复 4楼 rjsp
这个好复杂,我还没学到。那如果不用strlwr(),只是简单的把输入的pname全部变成小写字母,应该怎么做?谢谢
2017-03-22 15:52
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分:0 
回复 5楼 阿瞳正传
#include <iostream>
#include <algorithm>
#include <string>

int main( void )
{
    std::string s = "CarL";

    {
        std::transform( s.begin(), s.end(), s.begin(), &::tolower );
        std::cout << s << std::endl;
    }
    // 或
    //{
    //    std::for_each( begin(s), end(s), [](char& c){c=::tolower(c);} );
    //    std::cout << s << std::endl;
    //}
}
2017-03-22 15:58
快速回复:新手求教strlwr()
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015833 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved