반응형
문자열을 구성하는 character ascii code 합(sum) 구하기
테스트환경 : windows 7 32bit, visual studio 2013 express
참고 : http://www.cplusplus.com/reference/cstdio/printf/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <string.h> | |
int str_sum(char *); | |
int main() | |
{ | |
char *a = "abcdefgx"; | |
int sum = str_sum(a); | |
printf("sum of ascii code : \"%s\" ===> %d\n\n", a, sum); | |
} | |
// 매개변수 문자열을 이루고있는 char 들의 ascii 십진수 합 반환 | |
int str_sum(char *str) | |
{ | |
int sum = 0; | |
int len = strlen(str); | |
for (int i = 0; i < len ; i++) | |
{ | |
char *pos = str + i; // 출력할 char | |
sum += (int)(*pos); // ascii code 십진수 | |
// print char, ascii code, # 사용법 | |
printf("%c %d %x %#x %X %#X \n", *pos, *pos, *pos, *pos, *pos, *pos); | |
return sum; | |
} |
<결과>
반응형
'C & C++' 카테고리의 다른 글
c++ -- 가상함수 ( virtual function ), 동적바인딩 (0) | 2015.10.04 |
---|---|
QT 5.4 설치, hello world 프로그램 만들기. (0) | 2014.12.11 |
문자열 구성하는 character & ascii code 순서대로 출력하기 (0) | 2014.08.23 |
visual studio 2013 에서 C/C++ 명령행 인수 넣기 (0) | 2014.07.14 |
read only memory in C/C++ (0) | 2014.07.14 |