Printing a Character Array

Printing blocks of text (%s) C-String

print from the array specified, until it finds an element in the char array that has the value 0

C-Strings: a collection of characters is often referred to as a “string”

In C, strings are expected to be null terminated (C-Strings must be null terminated)

Meaning they end with the null character (The literal for this is: ‘\0’) (This is the value 0.)

We will call these C-Strings.

Remember:

Any other type of array is not terminated with a zero

Char arrays that contain C-Strings are terminated with a zero (the null character)

In initialization way

Initialized by using a simpler notation

It is printed as normal variables by combination of printf.

Beware the difference between %c (single char) and %s(string, array of char)

Character Array Sizes

Use scanf and %s to read into a char array by only one scanf

(don not need to scanf one char by one char unlike %c)

The array must be big enough to hold the number of characters

Restrict scanf to only read in the next 10 chars from input

%10s

Scansets

specify scanset specifiers with scanf by using % [ ]

Inside the [ ] brackets, we can specify the characters that scanf will process (or parse)

[A-Z]

(Stop scanning into the array when the next item in the input stream buffer is not an uppercase character…)

[A-Za-z]

(top scanning into the array when the next item in the input stream buffer is not an uppercase or lowercase character)

[0-9]

(Stop scanning into the array when the next item in the input stream buffer is not a digit character…)

…….

[^\n]

(Stop scanning when the user presses enter)

scanf("%*[^\n]");

*means scanf a something(here means” \n”), but do not save to the variables or array.

Means: scanf every char except the “\n”

C-String Length

#include <string. h>to access the strlen function

strlen takes in a char array and returns the number of characters in the C-String

The length of a C-String is not the same as the size of the char array in which it is stored (whitespace also be counted)

C-String Copying

#include <string. h>to access the strcpy function

strcpy takes in two char array parameters, the first one is the destination to copy to, the second one is the source to copy from.

C-String Concatenation

#include <string. h>to access the strcat function

char arrays that contain C-Strings are terminated with a zero

C-String convert to an int

atoi ()

convert a number, encoded as ASCII characters

atoi stands for “ASCII to int”

atof ()

converting ASCII text into a floating-point number

stands for “ASCII to floating-point”

Printing into a char Array

sprintf()

The array can then be used later… and then perhaps printed to the screen

sprintf needs to know what array to print into, and then the formatted string literal to print, followed by the variables

To convert an integer number into a C-String

(the char array must be big enough to hold all the ASCII digits)

If convert to float change the %d to %f

Practical Test Reminders, Character Arrays, C-Strings相关推荐

  1. c++ 数组换行_C语言的数组的构建与打印

    在许多程序中,数组很重要.数组可以作为一种存储多个相关项的便利方式. 数组(array)是按顺序存储的一系列类型相同的值,如10个char类型的字符或15个int类型的值.整个数组有一个数组名,通过整 ...

  2. MATLAB笔记:一些自嵌函数/built-in function

    文章目录 strvcat(不推荐)垂直串联字符串 ind2sub 索引值转换为下标 squeeze gcd(求最大公约数) fullfile physconst typecast(在不更改基础数据的情 ...

  3. Chapter 3. Strings, Vectors and Arrays -C++ Primer 5 notes

    Chapter 3. Strings, Vectors and Arrays What does built-in types include? chapter 2 array maybe more? ...

  4. Java数组– java.util.Arrays

    Java Arrays class consists exclusively of static methods that operates on array. Java Arrays类仅由对数组进行 ...

  5. arrays.sort(._Arrays.hashCode(Object [])与Objects.hash(Object…)

    arrays.sort(. 从JDK 1.5开始 , Arrays类提供了名为" hashCode "的重载static方法. 大多数重载方法都接受特定原始类型的数组,但是Arra ...

  6. Arrays.hashCode(Object [])与Objects.hash(Object…)

    从JDK 1.5开始 , Arrays类提供了名为" hashCode "的重载static方法. 大多数重载方法都接受特定原始类型的数组,但是Arrays.hashCode(Ob ...

  7. Arrays及Array

    这两个类都包含了很多用来操作Java数组的静态函数,分别定义如下: public final class Array extends Object public class Arrays extend ...

  8. Arrays.asList()抛错:UnsupportOperationException

    场景: Arrays.asList():后调用List的add/remove 方法会抛出UnsupportOperationException异常. //定义一个字符长度为5的字符串 String[] ...

  9. Arrays.asList() 详解

    [1. 要点] 该方法是将数组转化成List集合的方法. List list = Arrays.asList("a","b","c"); 注 ...

  10. Arrays.asList()避坑指南

    1. Arrays.asList() 该方法是将数组转化成List集合的方法. List<String> list = Arrays.asList("a"," ...

最新文章

  1. 零起步的Hadoop实践日记(更改hadoop数据存储位置)
  2. .NET6之MiniAPI(二十四):用Polly重试
  3. 这次使用一个最舒服的姿势插入HttpClient拦截器技能点
  4. 统计信号处理基础 - 估计与检测理论 估计部分习题3.7公式推导
  5. 小鱼易连全系新品正式发布 引爆音视频会议行业核聚变
  6. mysql 存储过程 错误码_mysql存储过程中的错误处理_mysql
  7. 什么是“云计算”,具体是怎么应用的?
  8. 特征图注意力_CV注意力机制论文阅读笔记
  9. 人工智能之产生式系统(c++实现)
  10. 贪心 学员 高阶机器学习 魔鬼·训练营
  11. Python解标准数独
  12. 二叉树的python实现
  13. 平板电脑安装软件_哪个便签可以横屏?平板电脑横屏便签软件推荐
  14. 【MockJS】使用MockJS模拟数据 (超级详细)
  15. 阿里云国际版免费试用套餐使用教程
  16. C语言中的sprintf()函数使用出错原因
  17. python123回文素数_平方回文素数
  18. springboot整合全文搜索引擎Elasticsearch | Spring Boot 28
  19. 腾讯安全“护航舰”亮相网安周,数实融合共筑产业安全防线
  20. Android App 设置系统时间,语言和时区、系统重启

热门文章

  1. Python tkinter库窗口化爬虫
  2. 【转】小玄子和小桂子
  3. HDU-4567-思维-Brilliant Programmers Show -13长沙邀请赛
  4. 达摩院python教程视频_达摩院官方刚刚发布了最新的Python教程,整套427集系统学习视频...
  5. 4个网页翻译工具,一键就能将网页英文翻译成中文
  6. Mac电池系统管理软件App Tamer好用吗?
  7. C语言中文网教程导航
  8. WMS库存盘点流程 WMS库存盘点计划
  9. 仓库盘点好方法,使用安卓盘点机PDA扫描商品条码进行超市盘点
  10. limits.conf文件_Limits.conf文件限制用户,在Linux中通过示例进行处理