strcat.cpp

来自「c语言教程源码」· C++ 代码 · 共 49 行

CPP
49
字号
//这个程序在本书所带软盘中。文件名为STRCAT.CPP
//这个程序利用字符串连接函数、数值到字符串的转换函数以及动态地址
//字节分配为输入的字符串加行标。

#include <iostream.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <conio.h>

void main(void)
{
	char *line;
	char text[81];		//定义数组
	int line_num = 1;

	do {
		cout << endl << "请输入一行字符串:";
		gets(text);

		line = new char[strlen(text)+3];	//动态字节分配

		itoa(line_num, line, 10);		//将十进制整数转换成字符串并赋予line

		strcat(line, ".");				//将字符串"."连接到line
		strcat(line, text);				//将text连接到line

		cout << line << endl;

		line_num++;

		cout << "你想继续运行程序吗?(y/n): ";
		cout << endl;

		delete[]line;					//释放line占据的内存字节
	} while(toupper(getche()) == 'Y');
}

/*下面是这个程序运行后的一个典型输出结果:
请输入一行字符串: 这是输入的一行中文字符串。
1.这是输入的一行中文字符串。
你想继续程序运行吗?(y/n): y

请输入一行字符串: This is a line of string in English.
2.This is a line of string in English.
你想继续程序运行吗?(y/n): n

*/

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?