![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
//
programmer_string_test.cpp : 定义控制台应用程序的入口点。
//
#include " stdafx.h "
#include " stdio.h "
#include < iostream >
#include " assert.h "
#include < string >
using namespace std;
#define MAX 50
/* 不用itoa函数,将整数转化成字符串数 */
void itoa_test()
{
int num,i,j = 0 ;
char a[MAX],b[MAX];
cout << " please input a interger: " << endl;
cin >> num;
while (num)
{
i = num % 10 ;
a[j] = i + ' 0 ' ;
j ++ ;
num /= 10 ;
};
a[j] = ' \0 ' ;
cout << " the inverted string is : " << a << endl;
// 求出来的a是倒转的,现将其再转回去
for (j = 0 ;j < strlen(a);j ++ )
{
b[j] = a[strlen(a) - j - 1 ];
}
b[j] = ' \0 ' ;
cout << " the converted string is : " << endl
<< b << endl;
}
/* 不用atoi函数,将字符串转换成整数 */
void atoi_test()
{
cout << " atoi_test: " << endl;
char a[MAX];
int i,num = 0 ,len;
cout << " please input a num string: " << endl;
cin.getline(a,MAX);
len = strlen(a);
for (i = 0 ;i < len;i ++ )
{
num = num * 10 + a[i] - ' 0 ' ;
}
cout << " the convered number is " << num << endl;
}
/* strcpy test */
char * strcpy_fun( char * dest, char * src)
{
int i,len;
len = strlen(src);
cout << " the len of src is " << len << endl;
for (i = 0 ;i < len;i ++ )
* dest ++=* src ++ ;
cout << " i is " << i << endl;
dest[i] = ' \0 ' ;
char c;
for (i = 0 ;i < 5 ;i ++ )
{
c = dest[i];
cout << c << endl;
}
return dest;
}
char * strcpy_answer( char * dest, const char * src)
{
assert((dest != NULL) && (src != NULL));
char * address = dest;
while (( * dest ++=* src ++ ) != ' \0 ' );
return address;
}
void strcpy_test()
{
cout << " strcpy_test: " << endl;
char dest[ 5 ],dest1[ 5 ],src[ 5 ] = " hi " ;
strcpy_fun(dest,src);
strcpy_answer(dest1,src);
cout << " the dest string is " << dest << endl;
cout << " the programmer answer is : " << dest1 << endl;
}
char * string_shift_fun( char * str, int steps)
{
char tmp[MAX];
int len,i;
len = strlen(str);
for (i = 0 ;i < steps;i ++ )
tmp[i] = str[len - steps + i];
for (i = 0 ;i < (len - steps);i ++ )
tmp[steps + i] = str[i];
tmp[len] = ' \0 ' ;
strcpy(str,tmp);
cout << " the tmp is : " << tmp << endl;
return str;
}
void string_shift_answer( char * pStr, int steps)
{
int n = strlen(pStr) - steps;
char tmp[MAX];
memcpy(tmp,pStr + n,steps);
memcpy(tmp + steps,pStr,n);
memcpy(pStr,tmp,strlen(pStr));
}
void string_shift_test()
{
/*
cout << "string_shift_test:" << endl;
char str[MAX],*str1;
cin.getline(str,10);
*/
char str[] = " hello " , * str1;
str1 = string_shift_fun(str, 2 );
cout << " the shifted string is: " << str1 << endl;
string_shift_answer(str, 2 );
cout << " shifted angain:the shifted string of programer answer is : " << str << endl;
}
// 找出字符串中出现的相同且长度最长的字符串,输出它的及其首字符的位置
int longest_same_string_answer()
{
string str,tep;
cout << " please input string : " << endl;
cin >> str;
for ( int i = str.length() - 1 ;i > 1 ;i -- )
for ( int j = 0 ;j < str.length();j ++ )
if (j + i <= str.length())
{
size_t t = 0 ;
size_t num = 0 ;
tep = str.substr(j,i);
t = str.find(tep);
num = str.rfind(tep);
if (t != num)
{
cout << tep << " " << t + 1 << endl;
return 0 ;
}
}
return 0 ;
}
// 返回主串中字符字串以后的所有字符
int substr_afterstr_fun( char * str, char * substr)
{
int len,sublen,i = 0 ,j = 0 ;
len = strlen(str);
sublen = strlen(substr);
while (i < len && j < sublen)
{
if (str[i] == substr[j])
{
i ++ ;
j ++ ;
}
else
{
i = i - j + 1 ;
j = 0 ;
}
}
if (j >= sublen)
return (i - sublen);
}
void substr_afterstr()
{
char str[] = " hellostrhaha " ,substr[] = " str " ;
int pos;
pos = substr_afterstr_fun(str,substr);
cout << " the string begin with the sub string is : " << str + pos << endl;
}
// 将一句话里的单词倒转,标点符号不倒转
void invert_fun( char * str)
{
int len ,i;
char tmp[MAX];
len = strlen(str);
for (i = 0 ;i < len;i ++ )
tmp[i] = str[len - 1 - i];
tmp[len] = ' \0 ' ;
strcpy(str,tmp);
}
void invert_word()
{
char str[] = " I come from tianjin. " ,tmp[MAX],word[MAX];
int len,i = 0 ,j = 0 ,k,m;
len = strlen(str);
invert_fun(str);
cout << " the totally invert string is: " << str << endl;
while (i < len)
{
word[j ++ ] = str[i ++ ];
if (str[i] == ' ' )
{
word[j] = ' \0 ' ;
invert_fun(word);
for (k = 0 ;k < strlen(word);k ++ )
str[i - j + k] = word[k];
j = 0 ;
i ++ ;
}
}
cout << " the invert string is : " << str << endl;
}
int _tmain( int argc, _TCHAR * argv[])
{
// write a user defined function about itoa
// itoa_test();
//
// atoi_test();
//
// strcpy_test();
//
// string_shift_test();
//
// longest_same_string_answer();
//
// substr_afterstr();
//
invert_word();
while ( 1 );
return 0 ;
}
//
#include " stdafx.h "
#include " stdio.h "
#include < iostream >
#include " assert.h "
#include < string >
using namespace std;
#define MAX 50
/* 不用itoa函数,将整数转化成字符串数 */
void itoa_test()
{
int num,i,j = 0 ;
char a[MAX],b[MAX];
cout << " please input a interger: " << endl;
cin >> num;
while (num)
{
i = num % 10 ;
a[j] = i + ' 0 ' ;
j ++ ;
num /= 10 ;
};
a[j] = ' \0 ' ;
cout << " the inverted string is : " << a << endl;
// 求出来的a是倒转的,现将其再转回去
for (j = 0 ;j < strlen(a);j ++ )
{
b[j] = a[strlen(a) - j - 1 ];
}
b[j] = ' \0 ' ;
cout << " the converted string is : " << endl
<< b << endl;
}
/* 不用atoi函数,将字符串转换成整数 */
void atoi_test()
{
cout << " atoi_test: " << endl;
char a[MAX];
int i,num = 0 ,len;
cout << " please input a num string: " << endl;
cin.getline(a,MAX);
len = strlen(a);
for (i = 0 ;i < len;i ++ )
{
num = num * 10 + a[i] - ' 0 ' ;
}
cout << " the convered number is " << num << endl;
}
/* strcpy test */
char * strcpy_fun( char * dest, char * src)
{
int i,len;
len = strlen(src);
cout << " the len of src is " << len << endl;
for (i = 0 ;i < len;i ++ )
* dest ++=* src ++ ;
cout << " i is " << i << endl;
dest[i] = ' \0 ' ;
char c;
for (i = 0 ;i < 5 ;i ++ )
{
c = dest[i];
cout << c << endl;
}
return dest;
}
char * strcpy_answer( char * dest, const char * src)
{
assert((dest != NULL) && (src != NULL));
char * address = dest;
while (( * dest ++=* src ++ ) != ' \0 ' );
return address;
}
void strcpy_test()
{
cout << " strcpy_test: " << endl;
char dest[ 5 ],dest1[ 5 ],src[ 5 ] = " hi " ;
strcpy_fun(dest,src);
strcpy_answer(dest1,src);
cout << " the dest string is " << dest << endl;
cout << " the programmer answer is : " << dest1 << endl;
}
char * string_shift_fun( char * str, int steps)
{
char tmp[MAX];
int len,i;
len = strlen(str);
for (i = 0 ;i < steps;i ++ )
tmp[i] = str[len - steps + i];
for (i = 0 ;i < (len - steps);i ++ )
tmp[steps + i] = str[i];
tmp[len] = ' \0 ' ;
strcpy(str,tmp);
cout << " the tmp is : " << tmp << endl;
return str;
}
void string_shift_answer( char * pStr, int steps)
{
int n = strlen(pStr) - steps;
char tmp[MAX];
memcpy(tmp,pStr + n,steps);
memcpy(tmp + steps,pStr,n);
memcpy(pStr,tmp,strlen(pStr));
}
void string_shift_test()
{
/*
cout << "string_shift_test:" << endl;
char str[MAX],*str1;
cin.getline(str,10);
*/
char str[] = " hello " , * str1;
str1 = string_shift_fun(str, 2 );
cout << " the shifted string is: " << str1 << endl;
string_shift_answer(str, 2 );
cout << " shifted angain:the shifted string of programer answer is : " << str << endl;
}
// 找出字符串中出现的相同且长度最长的字符串,输出它的及其首字符的位置
int longest_same_string_answer()
{
string str,tep;
cout << " please input string : " << endl;
cin >> str;
for ( int i = str.length() - 1 ;i > 1 ;i -- )
for ( int j = 0 ;j < str.length();j ++ )
if (j + i <= str.length())
{
size_t t = 0 ;
size_t num = 0 ;
tep = str.substr(j,i);
t = str.find(tep);
num = str.rfind(tep);
if (t != num)
{
cout << tep << " " << t + 1 << endl;
return 0 ;
}
}
return 0 ;
}
// 返回主串中字符字串以后的所有字符
int substr_afterstr_fun( char * str, char * substr)
{
int len,sublen,i = 0 ,j = 0 ;
len = strlen(str);
sublen = strlen(substr);
while (i < len && j < sublen)
{
if (str[i] == substr[j])
{
i ++ ;
j ++ ;
}
else
{
i = i - j + 1 ;
j = 0 ;
}
}
if (j >= sublen)
return (i - sublen);
}
void substr_afterstr()
{
char str[] = " hellostrhaha " ,substr[] = " str " ;
int pos;
pos = substr_afterstr_fun(str,substr);
cout << " the string begin with the sub string is : " << str + pos << endl;
}
// 将一句话里的单词倒转,标点符号不倒转
void invert_fun( char * str)
{
int len ,i;
char tmp[MAX];
len = strlen(str);
for (i = 0 ;i < len;i ++ )
tmp[i] = str[len - 1 - i];
tmp[len] = ' \0 ' ;
strcpy(str,tmp);
}
void invert_word()
{
char str[] = " I come from tianjin. " ,tmp[MAX],word[MAX];
int len,i = 0 ,j = 0 ,k,m;
len = strlen(str);
invert_fun(str);
cout << " the totally invert string is: " << str << endl;
while (i < len)
{
word[j ++ ] = str[i ++ ];
if (str[i] == ' ' )
{
word[j] = ' \0 ' ;
invert_fun(word);
for (k = 0 ;k < strlen(word);k ++ )
str[i - j + k] = word[k];
j = 0 ;
i ++ ;
}
}
cout << " the invert string is : " << str << endl;
}
int _tmain( int argc, _TCHAR * argv[])
{
// write a user defined function about itoa
// itoa_test();
//
// atoi_test();
//
// strcpy_test();
//
// string_shift_test();
//
// longest_same_string_answer();
//
// substr_afterstr();
//
invert_word();
while ( 1 );
return 0 ;
}