//===============================================================================
//程序的功能:写一个函数,将其中的 \t 都转换成4个空格
//程序的设计思路:此时需要对源串进行修改,因为不知道扩充的大小,所以需要定义一个临时数组,
//通过一次遍历,通过ascill将\t直接转换成4个空格,在对所获得的数据进行传回或者其他操作
//程序设计语言:c++
//=================================================================================
#include<iostream>
using namespace std ;
char* change(const char* str,int len) //注意此时传入的字符串是不能修改的,
{
int i = 0 ;
int j = 0 ;
//对输入的数据进行检查
if( str == NULL || len < 1)
return NULL ;
char* temp = new char[4*len+1] ; //最多情况就是传入的字符串都是\t,最后还需要加入一个\n作为字符串的结束标志
for(i = 0 ; i < len ; i++)
{
if(str[i] == '\t')
{
for(int k = 0 ; k < 4 ; k++)
temp[j++] = ' ';
}
else
temp[j++] = str[i] ;
}
temp[j] = '\n' ;
return temp ;
}
int main(void)
{
int res = 0 ;
char* t = NULL ;
char str[] = "gyu asdasj dcnajds adnc jdn dn nsc dn kas aosn ";
t = change(str,sizeof(str)/sizeof(char) );
//对输出的数据进行检查
if( t == NULL )
{
cout<<"func change error!"<<endl;
return res ;
}
cout<<"改变前的字符串:"<<str<<endl;
cout<<"改变后的字符串:"<<t<<endl ;
system("pause");
return res ;
}
//程序的功能:写一个函数,将其中的 \t 都转换成4个空格
//程序的设计思路:此时需要对源串进行修改,因为不知道扩充的大小,所以需要定义一个临时数组,
//通过一次遍历,通过ascill将\t直接转换成4个空格,在对所获得的数据进行传回或者其他操作
//程序设计语言:c++
//=================================================================================
#include<iostream>
using namespace std ;
char* change(const char* str,int len) //注意此时传入的字符串是不能修改的,
{
int i = 0 ;
int j = 0 ;
//对输入的数据进行检查
if( str == NULL || len < 1)
return NULL ;
char* temp = new char[4*len+1] ; //最多情况就是传入的字符串都是\t,最后还需要加入一个\n作为字符串的结束标志
for(i = 0 ; i < len ; i++)
{
if(str[i] == '\t')
{
for(int k = 0 ; k < 4 ; k++)
temp[j++] = ' ';
}
else
temp[j++] = str[i] ;
}
temp[j] = '\n' ;
return temp ;
}
int main(void)
{
int res = 0 ;
char* t = NULL ;
char str[] = "gyu asdasj dcnajds adnc jdn dn nsc dn kas aosn ";
t = change(str,sizeof(str)/sizeof(char) );
//对输出的数据进行检查
if( t == NULL )
{
cout<<"func change error!"<<endl;
return res ;
}
cout<<"改变前的字符串:"<<str<<endl;
cout<<"改变后的字符串:"<<t<<endl ;
system("pause");
return res ;
}