输入一个表示整数的字符串,把该字符串转换成整数并输出,例如输入字符串"345",则输出整数345。请完成函数StrToInt,实现字符串转换成整数的功能。
#include <stdio.h>
#include <assert.h>
#include <string.h>
static int strToInt(char *pcStr)
{
int iIdx = 0;
int iTmp = 0;
int iLen = 0;
assert(NULL != pcStr);
iLen = strlen(pcStr);
printf("Len is %d\n", iLen);
/* 去掉字符串前面的非数字字符 */
while ((pcStr[iIdx] < '0') || (pcStr[iIdx] >= '9'))
{
iIdx++ ;
}
while (iIdx < iLen)
{
iTmp *= 10;
iTmp += (pcStr[iIdx] - '0');
iIdx++ ;
}
return iTmp;
}
int main(int argc, char **argv)
{
char *pcStr = "123456";
int iResult = 0;
iResult = strToInt(pcStr);
printf("%d\n", iResult);
return 0;
}