c/c++字符轉換成字符串

以下方法來源於網絡,整理出來和初學者一起學習,舉例:

1.

2.

char ch='g',str[20];
sprintf( str, "abcdef%c", a );
printf( "%s", str );

打印的結果就是:
abcdefg

3.void chcat( char *str, char ch )
{
int len;
len = strlen(str);
str[len] = ch;
str[len+1] = 0;
}
直接在字符串末尾添加字符。



新手編程實例----生成wifi密碼字典的C++代碼:

// my_dic.cpp : 定義控制檯應用程序的入口點。
//

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
#include <stdio.h>

using namespace std;

const string path1 = "C:\\Users\\TheKing\\Desktop\\dic_1.txt";
const string path2 = "C:\\Users\\TheKing\\Desktop\\dic_2.txt";
const string path3 = "C:\\Users\\TheKing\\Desktop\\dic_3.txt";
const string path4 = "C:\\Users\\TheKing\\Desktop\\dic_4.txt";
const string path5 = "C:\\Users\\TheKing\\Desktop\\dic_5.txt";

const string path2_1 = "C:\\Users\\TheKing\\Desktop\\dic2_1.txt";
const string path2_2 = "C:\\Users\\TheKing\\Desktop\\dic2_2.txt";
const string path2_3 = "C:\\Users\\TheKing\\Desktop\\dic2_3.txt";
const string path2_4 = "C:\\Users\\TheKing\\Desktop\\dic2_4.txt";
const string path2_5 = "C:\\Users\\TheKing\\Desktop\\dic2_5.txt";

 char tel[12] = {'1','8','3','7','1'};
 char tel2_1[13] = {'0','1','8','3','7','1'};
 char tel2_2[14] = {'0','0','1','8','3','7'};
 char tel2_3[15] = {'0','0','0','1','8','3''};
 char tel2_4[16] = {'0','0','0','0','1','8'};

void c_dic1()   //末尾一個字母
{
	ofstream fout(path1);
	if(fout)
		cout<<"Create C:\\Users\\TheKing\\Desktop\\dic_1.txt sucessful "<<endl;
	else
		cout<<"FAILED create dic1!"<<endl;

	char head = 20;  //41~122   = 81

	if(!fout)
	{
		cout<<"Open Failed "<<endl;
		cout<<endl;
	}
	else
	{	cout<<"Open Successed dic1"<<endl;
			for(int i = 0;i<106;i++)
			{
				head += 1;
				tel[11] = head;	
				string str(tel,12);
				fout<<str<<endl;
			}
		
	}
	fout.close();   //關閉流文件
		
	cout<<endl;
}

void c_dic2()   //末尾一個字母
{
	ofstream fout(path2);
	if(fout)
		cout<<"Create C:\\Users\\TheKing\\Desktop\\dic_2.txt sucessful "<<endl;
	else
		cout<<"FAILED create dic2!"<<endl;

	char head1 = 20;  //41~122   = 81
	char head2 = 20;
	int i = 0,j = 0;

	if(!fout)
	{
		cout<<"Open Failed "<<endl;
		cout<<endl;
	}
	else
	{	cout<<"Open Successed dic2"<<endl;

	for(i = 0;i<106;i++)
	{
		head1 += 1;
		tel[11] = head1;
		
		for(j = 0;j<106;j++)
		{
			head2 += 1;
			tel[12] = head2;
			string str(tel,13);
			fout<<str<<endl;
		}
		head2 = 20;				
	}
	}
	fout.close();   //關閉流文件

	cout<<endl;
}

void c_dic3()   //末尾一個字母
{
	ofstream fout(path3);
	if(fout)
		cout<<"Create C:\\Users\\TheKing\\Desktop\\dic_3.txt sucessful "<<endl;
	else
		cout<<"FAILED create dic3!"<<endl;

	char head1 = 20;  //41~122   = 81
	char head2 = 20;
	char head3 = 20;
	int i = 0,j = 0,k = 0;

	if(!fout)
	{
		cout<<"Open Failed "<<endl;
		cout<<endl;
	}
	else
	{	cout<<"Open Successed dic3"<<endl;

	for(i = 0;i<106;i++)
	{
		head1 += 1;
		tel[11] = head1;

		for(j = 0;j<106;j++)
		{
			head2 += 1;
			tel[12] = head2;
			for(k = 0;k<20;k++)
			{
				head3 += 1;
				tel[13] = head3;
				string str(tel,14);
				fout<<str<<endl;
			}
			head3 = 20;
		}
		head2 = 20;				
	}
	}
	fout.close();   //關閉流文件

	cout<<endl;
}

void c_dic4()   //末尾一個字母
{
	ofstream fout(path4);
	if(fout)
		cout<<"Create C:\\Users\\TheKing\\Desktop\\dic_4.txt sucessful "<<endl;
	else
		cout<<"FAILED create dic4!"<<endl;

	char head1 = 64;  //41~122   = 81
	char head2 = 64;
	char head3 = 64;
	char head4 = 64;
	int i = 0,j = 0,k = 0,l = 0;

	if(!fout)
	{
		cout<<"Open Failed "<<endl;
		cout<<endl;
	}
	else
	{	cout<<"Open Successed dic3"<<endl;

	for(i = 0;i<64;i++)
	{
		head1 += 1;
		tel[11] = head1;

		for(j = 0;j<64;j++)
		{
			head2 += 1;
			tel[12] = head2;
			for(k = 0;k<64;k++)
			{
				head3 += 1;
				tel[13] = head3;
				for(l = 0;l<32;l++)
				{
					head4 += 1;
					tel[14] = head4;
						string str(tel,15);
					fout<<str<<endl;
				}
				head4 = 64;
			}
			head3 = 64;
		}
		head2 = 64;				
	}
	}
	fout.close();   //關閉流文件



	if(!fout)
	{
		cout<<"Open Failed "<<endl;
		cout<<endl;
	}
	else
	{	cout<<"Open Successed dic3"<<endl;
	for(i = 0;i<64;i++)
	{
		head1 += 1;
		tel[11] = head1;

		for(j = 0;j<64;j++)
		{
			head2 += 1;
			tel[12] = head2;
			for(k = 0;k<64;k++)
			{
				head3 += 1;
				tel[13] = head3;
				for(l = 32;l<64;l++)
				{
					head4 += 1;
					tel[14] = head4;
					string str(tel,15);
					fout<<str<<endl;
				}
				head4 = 64;
			}
			head3 = 64;
		}
		head2 = 64;				
	}
	}
	fout.close();   //關閉流文件

	cout<<endl;
}

void c_dic5()   //末尾一個字母
{
	ofstream fout(path5);
	if(fout)
		cout<<"Create C:\\Users\\TheKing\\Desktop\\dic_5.txt sucessful "<<endl;
	else
		cout<<"FAILED create dic5!"<<endl;

	char head1 = 96;  //41~122   = 81
	char head2 = 96;
	char head3 = 96;
	char head4 = 96;
	char head5 = 96;
	int i = 0,j = 0,k = 0,l = 0,m = 0;

	if(!fout)
	{
		cout<<"Open Failed "<<endl;
		cout<<endl;
	}
	else
	{	cout<<"Open Successed dic5"<<endl;
	//fout.close();   //關閉流文件
	for(i = 0;i<26;i++)
	{
		head1 += 1;
		tel[11] = head1;

		for(j = 0;j<26;j++)
		{
			head2 += 1;
			tel[12] = head2;
			for(k = 0;k<26;k++)
			{
				head3 += 1;
				tel[13] = head3;
				for(l = 0;l<26;l++)
				{
					head4 += 1;
					tel[14] = head4;
					for(m = 0;m<26;m++)
					{
						head5 += 1;
						tel[15] = head5;
						
						string str(tel,16);
						fout<<str<<endl;
						
					}
					head5 = 96;
					
				}
				head4 = 96;
			}
			head3 = 96;
		}
		head2 = 96;				
	}
	}
			
	fout.close();   //關閉流文件

	cout<<endl;
}

void c_dic2_1()   //末尾一個字母
{
	ofstream fout(path2_1);
	if(fout)
		cout<<"Create C:\\Users\\TheKing\\Desktop\\dic2_1.txt sucessful "<<endl;
	else
		cout<<"FAILED create dic2_1!"<<endl;

	char head = 64;  //41~122   = 81

	if(!fout)
	{
		cout<<"Open Failed "<<endl;
		cout<<endl;
	}
	else
	{	cout<<"Open Successed dic2_1"<<endl;
	for(int i = 0;i<64;i++)
	{
		head += 1;
		tel2_1[0] = head;	
		string str(tel2_1,12);
		fout<<str<<endl;
	}

	}
	fout.close();   //關閉流文件

	cout<<endl;
}

void c_dic2_2()   //末尾一個字母
{
	ofstream fout(path2_2);
	if(fout)
		cout<<"Create C:\\Users\\TheKing\\Desktop\\dic2_2.txt sucessful "<<endl;
	else
		cout<<"FAILED create dic2_2!"<<endl;

	char head1 = 64;  //41~122   = 81
	char head2 = 64;
	int i = 0,j = 0;

	if(!fout)
	{
		cout<<"Open Failed "<<endl;
		cout<<endl;
	}
	else
	{	cout<<"Open Successed dic2_2"<<endl;

	for(i = 0;i<64;i++)
	{
		head1 += 1;
		tel2_2[0] = head1;

		for(j = 0;j<64;j++)
		{
			head2 += 1;
			tel2_2[1] = head2;
			string str(tel2_2,13);
			fout<<str<<endl;
		}
		head2 = 64;				
	}
	}
	fout.close();   //關閉流文件

	cout<<endl;
}

void c_dic2_3()   //末尾一個字母
{
	ofstream fout(path2_3);
	if(fout)
		cout<<"Create C:\\Users\\TheKing\\Desktop\\dic2_3.txt sucessful "<<endl;
	else
		cout<<"FAILED create dic2_3!"<<endl;

	char head1 = 96;  //41~122   = 81
	char head2 = 96;
	char head3 = 96;
	int i = 0,j = 0,k = 0;

	if(!fout)
	{
		cout<<"Open Failed "<<endl;
		cout<<endl;
	}
	else
	{	cout<<"Open Successed dic2_3"<<endl;

	for(i = 0;i<26;i++)
	{
		head1 += 1;
		tel2_3[0] = head1;

		for(j = 0;j<26;j++)
		{
			head2 += 1;
			tel2_3[1] = head2;
			for(k = 0;k<26;k++)
			{
				head3 += 1;
				tel2_3[2] = head3;
				string str(tel2_3,14);
				fout<<str<<endl;
			}
			head3 = 96;
		}
		head2 = 96;				
	}
	}
	fout.close();   //關閉流文件

	cout<<endl;
}

void c_dic2_4()   //末尾一個字母
{
	ofstream fout(path2_4);
	if(fout)
		cout<<"Create C:\\Users\\TheKing\\Desktop\\dic2_4.txt sucessful "<<endl;
	else
		cout<<"FAILED create dic4!"<<endl;

	char head1 = 96;  //41~122   = 81
	char head2 = 96;
	char head3 = 96;
	char head4 = 96;
	int i = 0,j = 0,k = 0,l = 0;

	if(!fout)
	{
		cout<<"Open Failed "<<endl;
		cout<<endl;
	}
	else
	{	cout<<"Open Successed dic3"<<endl;

	for(i = 0;i<26;i++)
	{
		head1 += 1;
		tel2_4[0] = head1;

		for(j = 0;j<26;j++)
		{
			head2 += 1;
			tel2_4[1] = head2;
			for(k = 0;k<26;k++)
			{
				head3 += 1;
				tel2_4[2] = head3;
				for(l = 0;l<26;l++)
				{
					head4 += 1;
					tel2_4[3] = head4;
					string str(tel2_4,15);
					fout<<str<<endl;
				}
				head4 = 96;
			}
			head3 = 96;
		}
		head2 = 96;				
	}
	}
	fout.close();   //關閉流文件



}
int main()
{
	
	//c_dic1();
	//c_dic2();
	//c_dic3();
	//c_dic4();
	//c_dic5();

	//c_dic2_1();
	//c_dic2_2();
	c_dic2_3();
	c_dic2_4();
	system("pause");

}


發佈了24 篇原創文章 · 獲贊 2 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章