C语言数组、字符串
- 一维数组
- 数组必须先定义,然后使用
- C语言规定只能逐个引用数组元素而不能一次引用整个数组
- 不能直接将一个数组赋给另一个数组
- 不能为数组整体赋值
- 字符数组与字符串
- 字符串常量是由一对双引号括起来的字符序列
- 字符串中每个字符作为一个数组元素存储(占一个字节)
- 对于wchar_t类型的字符常量,需要在字符前面加上L,在实际使用中,wchar_t类型的字符前面可以不用加L
- 对于wchar_t类型的字符串常量,需要在字符串前面加上L。
- 字符数组的输入和输出
- 获取字符串长度
- strlen(str)
- strnlen_s(str,100)
- wchar_t(str)
- wcsnlen_t(str,100)
- 字符串拷贝
- strcpy(str)
- strcpy_s(str,100)
- wcscpy(str)
- wcscpy_s(str,100)
- 字符串比较
- strcmp(str,str2)
- wcscmp(str,str2)
- 字符串连接
- strcat(str,str2)
- strcat_s(str,100,str2)
- wcscat(str,str2)
- wcscat_s(str,100,str2)
- _getch()函数包含在conio.h的头文件中。
- 避免数据溢出
- 浮点数总采用double类型
- 整数总采用int类型
代码整理
3-1
#include <stdio.h>
int main()
{
int i, j, k;
printf("\n");
for (i = 1; i < 5; i++)
for (j = 1; j < 5; j++)
for (k = 1; k < 5; k++)
if (i != k&&i != j&&j != k)
printf("%d,%d,%d\n", i, j, k);
}
3-2
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a;
scanf_s("%d", &a);
//if (a>0)
//{
//printf("yes");
//}
//else
//{
//printf("no");
//}
/*if (a%2==0)
{
printf("yes");
}
else
{
printf("no");
}*/
/*if (a % 4 == 0)
{
if (a%400==0)
{
printf("run");
}
else
{
if (a % 100 != 0)
{
printf("run");
}
else
{
printf("ping");
}
}
}
else
{
printf("ping");
}*/
/*if (a==1)
{
printf("星期一\n");
}
else if (a==2)
{
printf("星期二\n");
}
else if (a==3)
{
printf("星期三\n");
}
else if (a == 4)
{
printf("星期四\n");
}
else if (a == 5)
{
printf("星期五\n");
}
else if (a == 6)
{
printf("星期六\n");
}
else if (a == 7)
{
printf("星期日\n");
}
*/
switch (a)
{
case 1:
printf("星期一\n");
break;
case 2:
printf("星期二\n");
break;
case 3:
printf("星期三\n");
break;
case 4:
printf("星期四\n");
break;
case 5:
printf("星期五\n");
break;
case 6:
printf("星期六\n");
break;
case 7:
printf("星期日\n");
default:
printf("error\n");
break;
}
system("pause");
return 0;
}
3-3
// day3-03.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <string>
#include <stdio.h>
#include <stdlib.h>
int _tmain(int argc, _TCHAR* argv[])
{
int a1,a2,b1, b2;
char strChrA[100] = "abcdef";
a1=strlen(strChrA);
a2=strnlen_s(strChrA, 100);
printf("%d%d\n", a1, a2);
wchar_t strChrAB[100] = L"abcdef";
b1=wcslen(strChrAB);
b2=wcsnlen_s(strChrAB,100);
printf("%d%d\n", b1, b2);
system("pause");
return 0;
}
3-4
// day3-04.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
int _tmain(int argc, _TCHAR* argv[])
{
int c = 'A';
putchar(c);
putchar('\n');
putchar('A');
putchar('\n');
putchar(65);
putchar('\n');
putchar('5');
putchar('\n');
putchar('5' + 1);
system("pause");
return 0;
}
3-5
// day3-05.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <conio.h>//注意_getch()的头文件是这个
int _tmain(int argc, _TCHAR* argv[])
{
char ch =_getch();
return 0;
}
3-6
// day3-06.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <string>
int _tmain(int argc, _TCHAR* argv[])
{
char strChA[100];
char strChB[] = "xxxxx";
strcpy_s(strChA, 100, strChB);
printf("%s\n", strChA);
wchar_t strChc[100];
wchar_t strChd[] = L"yyyyy";
wcscpy_s(strChc,20, strChd);
wprintf(L"%s\n", strChc);
system("pause");
return 0;
}
3-7
// day3-07.cpp : 定义控制台应用程序的入口点。
//字符串比较
#include "stdafx.h"
#include <string>
int _tmain(int argc, _TCHAR* argv[])
{
char stra[] = "aaaaa";
char strb[] = "bbbbb";
int nRet = strcmp(stra, strb);
printf("%d\n", nRet);
wchar_t strc[] = L"yyyy";
wchar_t strd[] = L"yyyy";
nRet = wcscpy_s(strc, 5, strd);
printf("%d\n", nRet);
system("pause");
return 0;
}
3-8
// day3-08.cpp : 定义控制台应用程序的入口点。
//字符串连接
#include "stdafx.h"
#include <stdlib.h>
#include <string.h>
int _tmain(int argc, _TCHAR* argv[])
{
char str1[30] = { "People's Republic of " };
char str2[] = { "China" };
strcat_s(str1, 30,str2);
printf("%s\n", str1);
system("pause");
return 0;
}
3-9
// day3-09.cpp : 定义控制台应用程序的入口点。
//再谈输入输出
#include "stdafx.h"
#include <stdlib.h>
int _tmain(int argc, _TCHAR* argv[])
{
char ch;
scanf_s("%c", &ch, 1);
char string[100];
scanf_s("%s", &string, 100);
wchar_t string2[100];
wscanf_s(L"%s", &string2, 100);
system("pause");
return 0;
}
3-10
// day3-11.cpp : 定义控制台应用程序的入口点。
//使用if与goto构造循环结构
#include "stdafx.h"
int _tmain(int argc, _TCHAR* argv[])
{
int i = 1, sum = 0;
loop:
if (i <= 100)
{
sum = sum + i;
i++;
goto loop;
}
printf("%d\n", sum);
return 0;
}
3-11
//day3-10.cpp : 定义控制台应用程序的入口点。
//switch-case 实现会移动的坐标
#include "stdafx.h"
#include <conio.h>
int _tmain(int argc, _TCHAR* argv[])
{
int zbX = 10, zbY = 10;
sign:
char ch = _getch();
switch (ch)
{
case 'W':
case 'w':
{
zbY--;
printf("向上:X=%d,Y=%d", zbX, zbY);
break;
}
case 's':
case 'S':
{
zbY++;
printf("向下:X=%d,Y=%d", zbX, zbY);
break;
}
case 'a':
case 'A':
{
zbX--;
printf("向左:X=%d,Y=%d", zbX, zbY);
break;
}
case 'd':
case 'D':
{
zbX++;
printf("向右:X=%d,Y=%d", zbX, zbY);
break;
}
default:
break;
}
goto sign;
return 0;
return 0;
}