(Original) An interesting question about the C-language string (C/C ++) (c)

Source: Internet
Author: User
Abstract
This is a younger brother's homework. It is an interesting question about the C-language string.

Introduction
A = 1, B = 2, c = 3... if the input string ABC is used as this type, it indicates 1 + 2 + 3 = 6. If the input string ABCD is used as the input string, it indicates 1 + 2 + 3 + 4 = 10.

C. Yan

1 /*  
2 (C) oomusou 2008 Http://oomusou.cnblogs.com
3
4 Filename: cstring_sum.cpp
5 Compiler: Visual C + + 8.0
6 Description: Demo how to use abstract base class simulate Interface
7 Release: 03/16/2007 1.0
8 */
9 # Include < Stdio. h >
10
11 Void Func ( Char   * S ){
12 Int Sum =   0 ;
13
14 While ( * S)
15 Sum + = ( * S ++   -   96 );
16
17 Printf ( " % D " , Sum );
18 }
19
20 Int Main (){
21 Char S [ 27 ];
22 Scanf ( " % S " , S );
23 Func (s );
24 }

Results

ABC
6

Lines 14 and 15

While ( * S)
Sum + = ( * S ++   -   96 );

Because the C statement string ends with '\ 0', we use while (* s! = '\ 0'), and because C uses the "not 0 is true" feature, it can be omitted as while (* s, it must be difficult to see this method, but in C, this method is an empty comment, there are no questions about "it is not easy to answer 』.

A = 1, B = 2, c = 3... should I make a look-up table for a partition column? A c-character has a special feature: "A character is also equal to its ASCII value", that is, a = 97, B = 98, c = 99, so as long as the ASCII value of each character is 96, it becomes 1, 2, 3.

* S ++, which is also a common method in C programming languages. In the C program language, this parameter is used everywhere. In other words, it refers to the value of s first, after the selection, it refers to the "+ 1" standard. Beginners must not understand this method, but this is already one of the cultures of C.

Conclusion
Although it is a short program, it can be seen that the old team has worked hard for this job, including a lot of negative comments. In the C programming language, while has more opportunities than for, mainly because if the while with pointer can use the * s ++ operator method, while (* s) can be used in the string, which is where c statements are written in other languages.

See also
(Original) How can I print a single character? (C)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.