1. 程式人生 > >實現字串到整數的轉換,例如輸入字串12345,則輸出數字12345。

實現字串到整數的轉換,例如輸入字串12345,則輸出數字12345。

#include <stdio.h>
#include <windows.h>
#include <math.h>


/*字串到整型的轉換*/
int swith(char p[],size_t sz)
{
	int i = 0;
	int sum = 0;
	for (i; i < sz; i++){
		//sum += (p[i]-'0') * pow(10, sz - i - 1);
		sum = sum * 10 + p[i] - '0';
	}
	printf("%d\n", sum);
	return 0;
}
int main()
{
	char p[] = "12345";
	int sz = strlen(p);
	swith (p, sz);


	system("pause");
	return 0;
}