Chapter 1 Working with strings

1.1 Input

 1 // ask for a person‘s name, and greet the person
 2 #include <iostream>
 3 #include <string>
 4
 5 int main()
 6 {
 7     // ask for a person‘s name
 8     std::cout << "Please enter your name : " ;
 9
10     // read the name
11     std::string name; // define name
12     std::cin >> name; // read into
13
14     // write a greeting
15     std::cout << "Hello, "<< name << " ! " << std::endl;
16     return 0;
17 }
18
19 /*
20 在VC6.0中的运行的结果是:
21 --------------------------------
22 Please enter your name : Andrew
23 Hello, Andrew !
24 --------------------------------
25 */

1.2 Framing a name

 1 // ask for a person‘s name, and generate a framed greeting
 2 #include <iostream>
 3 #include <string>
 4
 5 int main()
 6 {
 7     std::cout << "Please enter your name : " ;
 8     std::string name;
 9     std::cin >> name;
10
11     // build the message that we intend to write
12     const std::string greeting = "Hello , " + name + "!" ;
13
14     // build the second and fourth lines of the output
15     const std::string spaces(greeting.size(), ‘ ‘);
16     const std::string second = "*" + spaces + "*";
17
18     // build the first and fifth lines of the output
19     const std::string first(second.size(), ‘*‘);
20
21     // write it all
22     std::cout << std::endl;
23     std::cout << first << std::endl;
24     std::cout << second << std::endl;
25     std::cout << "*" << greeting << "*" << std::endl;
26     std::cout << second << std::endl;
27     std::cout << first << std::endl;
28     return 0;
29 }
30
31 /*
32 在VC6.0中的运行的结果是:
33 --------------------------------
34 Please enter your name : X_man
35
36 ****************
37 *              *
38 *Hello , X_man!*
39 *              *
40 ****************
41 --------------------------------
42 */
时间: 2024-10-12 20:42:58

Chapter 1 Working with strings的相关文章

CareerCup chapter 1 Arrays and Strings

1.Implement an algorithm to determine if a string has all unique characters What if you can not use additional data structures? The length of ACSII code of a character is 8, so we can build a array, the length is 260, to represent the hash table of a

UVA - 10340 - All in All (字符串处理!)

题目链接:All in All Problem E All in All Input: standard input Output: standard output Time Limit: 2 seconds Memory Limit: 32 MB You have devised a new encryption technique which encodes a message by inserting between its characters randomly generated st

Cracking the coding interview汇总目录

很久之前刷的CTCI的题目,都快忘记了,做个分类汇总,再重新好好复习一遍. Chapter 1 | Arrays and Strings 1.1 Implement an algorithm to determine if a string has all unique characters. What if you can not use additional data structures? 1.2 Write code to reverse a C-Style String. (C-Str

uvaoj 401 Palindromes

************************************************题目描述*************************************************** UVA - 401 Palindromes Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description A regular palindrome is

Java性能提示(全)

http://www.onjava.com/pub/a/onjava/2001/05/30/optimization.htmlComparing the performance of LinkedLists and ArrayLists (and Vectors) (Page last updated May 2001, Added 2001-06-18, Author Jack Shirazi, Publisher OnJava). Tips: ArrayList is faster than

CareerCup All in One 题目汇总 (未完待续...)

Chapter 1. Arrays and Strings 1.1 Unique Characters of a String 1.2 Reverse String 1.3 Permutation String 1.4 Replace Spaces 1.5 Compress String 1.6 Rotate Image 1.7 Set Matrix Zeroes 1.8 String Rotation Chapter 2. Linked Lists 2.1 Remove Duplicates

CareerCup之1.1字符串中字符判重

[题目] Chapter 1 | Arrays and Strings 原文: 1.1 Implement an algorithm to determine if a string has all unique characters. What if you can not use additional data structures? 译文: 实现一个算法来判断一个字符串中的字符是否唯一(即没有重复).不能使用额外的数据结构. (即只使用基本的数据结构) [分析] [思路一]首先,我们要搞清

CareerCup All in One 题目汇总

Chapter 1. Arrays and Strings 1.1 Unique Characters of a String 1.2 Reverse String 1.3 Permutation String 1.4 Replace Spaces 1.5 Compress String 1.6 Rotate Image 1.7 Set Matrix Zeroes 1.8 String Rotation Chapter 2. Linked Lists 2.1 Remove Duplicates

Errata for C# 6.0 in a Nutshell

Version Location Description PDF Page 15-16 list of reserved keywords and list of contextual keywords The keyword 'in' appears in both the reserved keyword list and the contextual keyword list. Note from the Author or Editor: The 'in' keyword should