简体   繁体   English

C ++如何将字符串转换为char数组并检查数字?

[英]C++ how to convert string to char array and check for digits?

how do I do this? 我该怎么做呢? i want it to check for numbers 我希望它检查数字

cout<<"Enter your first number: ";
std::cin >> dblNumOne;
int i=0;
char str[]=dblNumkOne;
while (str[i])
{
    if (isalpha(str[i])) printf ("character %c is alphabetic\n",str[i]);
    else printf ("character %c is not alphabetic\n",str[i]);
    i++;
}

Simple, just iterate through the std::string as such: 简单,只需遍历std :: string:

std::string dblNumOne;
std::cin >> dblNumOne;

for(unsigned int i = 0; i < dblNumOne.length(); i++)
{
    if (isalpha(dblNumOne[i]))
    {
        printf ("character %c is alphabetic\n", dblNumOne[i]);
    }
    else
    {
        printf ("character %c is not alphabetic\n", dblNumOne[i]);
    }
}

One way is to use string instead of char, because string has a length function 一种方法是使用字符串而不是char,因为string具有length函数

so like this: 像这样:

std::string dblNumOne;
std::cout<<"Enter your first number: ";
std::cin >> dblNumOne;
std::cout << dblNumOne.length() << endl;

If you have to use char, then you can simply do it by checking '\\0' value which stands for end of string. 如果你必须使用char,那么你可以通过检查代表字符串结尾的'\\0'值来做到这一点。

My approach would be use the string and get the length of it and then convert it to char array by using c_str() function. 我的方法是使用字符串并获取它的长度,然后使用c_str()函数将其转换为char数组。

1) Convert the string to a char array: 1)将字符串转换为char数组:

strcpy(charArray,stringArray.c_str());

Source: http://v2.cplusplus.com/forum/windows/71633/ 资料来源: http//v2.cplusplus.com/forum/windows/71633/

2) Print out whether it's a digit or not using isdigit : 2)使用isdigit打印出是否为数字:

while (charArray[i]) {
    if (isdigit(charArray[i])){
        printf ("character %c is a digit\n",str[i]);
    } else {
        printf ("character %c is not a digit\n",str[i]);
    }
    i++;
}

Remember to click the grey check mark (the ✓) to the left of my answer if it answers your question or led you to the answer! 如果它回答了你的问题或者引导你回答,请记得点击我答案左边的灰色复选标记(✓)!

If you want to check for numbers, you could try this: 如果您想检查数字,可以试试这个:

std::string str = "";
std::cout<<"enter string>>";
std::cin>>str;

int is_number = 0; 
char filter[] = "0123456789";

for(int n=0; n<str.size(); ++n){
   for(int i=0; i<10; ++i){

      if(str[n] == filter[i]){
          ++is_number;
          break; // found match, filter next char in str
      }else if(str[n] != filter[i] && i == 9){
          std::cout<<"'"<<str[n]<<"' is not a digit!\n";
      }
   }
}

if(is_number == str.size()){
    std::cout<<"You entered a number";
}else std::cout<<"You didn't enter a number!";

or to recognize decimal numbers you could try this: 或者识别十进制数字你可以试试这个:

int is_number = 0, is_decimal = 0; 
char filter[] = ".0123456789";

for(int n=0; n<str.size(); ++n){
   for(int i=0; i<11; ++i){

      if(str[n] == filter[i]){
          if(filter[i] == '.'){
            ++is_decimal;
          }
          ++is_number;
          break;
      }
   }
}// for decimal

if(is_number == str.size() && is_decimal <= 1){
    std::cout<<"You entered a number";
}else std::cout<<"You didn't enter a number!";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM