c语言连接字符串常见问题,常见c语言字符串问题

字符串是程序员求职笔试中必考题型,很能考查出编程的基础。下文选取了几个常见的考题和大家进行分享。

1、编写函数,实现把一个char组成的字符串循环右移n位。如abcdehi,n=2。则输出hiabcde。

#include "iostream"

using namespace std;

const int MAX_LEN = 20;

void LoopMove(char* cpStr, int iSteps)

{

//注意,在整个处理过程中,cpStr的最后字符都没有涉及处理

char cTempArray[MAX_LEN];

size_t szStrLength = strlen(cpStr);

size_t in = szStrLength -iSteps;

memcpy(cTempArray, cpStr + in, iSteps);

memcpy(cTempArray + iSteps, cpStr, in);

memcpy(cpStr, cTempArray, szStrLength);

cTempArray[szStrLength + 1] = '\0';

cout << cTempArray << endl;

}

int main()

{

char ctemp[] = "abcdefghi";

LoopMove(ctemp, 2);

cout << ctemp << endl;

return 1;

}

2、输入一行字符串,找出其中出现的相同且长度最长的字符串,输出它及其首字符的位置。如yyabcdabjcabceg,则输出为abc,3。

大体思路:把字符串yyabcdabjcabceg拆解:

yyabcdabjcabceg

yabcdabjcabceg

abcdabjcabceg

...

ceg

eg

g

然后对字符串进行排序,比较相邻字符串的前驱,求最长的共公前驱。

在我们的程序中的体现,我们没有用这种方法,因为这种方法在排序中会用很多时间。我们借用了C++的实现函数find来巧妙的实现。

注:basic_string::substr

basic_string substr(size_type pos = 0, size_type n =

npos) const;

The member function returns an object whose

controlled sequence is a copy of up to n elements of the controlled

sequence beginning at position pos.

返回一个从指定位置开始,并具有指定长度的子字符串。

参数

pos 必选。所需的子字符串的起始位置。字符串中第一个字符的索引为 0。

n 可选项。返回的子字符串中包含的字符数。

备注 如果 n 为 0

或负数,将返回一个空字符串。如果没有指定该参数,则子字符串将延续到字符串的结尾。

在VS中测试,如是n是负数或大于主串的总长度,则输出是pos开始到主串末尾的字符。

示例代码

大体思路:把字符串yyabcdabjcabceg拆解:

yyabcdabjcabceg

yabcdabjcabceg

abcdabjcabceg

...

ceg

eg

g

然后对字符串进行排序,比较相邻字符串的前驱,求最长的共公前驱。

在我们的程序中的体现,我们没有用这种方法,因为这种方法在排序中会用很多时间。我们借用了C++的实现函数find来巧妙的实现。

注:basic_string::substr

basic_string substr(size_type pos = 0, size_type n = npos) const;

The member function returns an object whose controlled sequence is a copy of up to n elements of the controlled sequence beginning at position pos.

返回一个从指定位置开始,并具有指定长度的子字符串。

参数

pos 必选。所需的子字符串的起始位置。字符串中第一个字符的索引为 0。

n 可选项。返回的子字符串中包含的字符数。

备注 如果 n 为 0 或负数,将返回一个空字符串。如果没有指定该参数,则子字符串将延续到字符串的结尾。

在VS中测试,如是n是负数或大于主串的总长度,则输出是pos开始到主串末尾的字符。

示例代码

#include

"iostream"

#include "string"

using namespace std;

int main()

{

string strInput;

cout << "Input a string: " << endl;

cin >> strInput;

string strTemp;

for (size_t i = strInput.length() - 1; i >

1; i--)

{

for (size_t j = 0; j < strInput.length();

j++)

{

if ((i + j) <=

strInput.length())

{

size_t szForw =

0;

size_t szBacw

= 0;

strTemp = strInput.substr(j, i);

szForw = strInput.find(strTemp);

szBacw = strInput.rfind(strTemp);

if (szBacw !=

szForw)

{

cout << strTemp <<

" " << szForw + 1 <<

endl;

return 0;

}

}

}

}

return 1;

}

#include "iostream"

#include "string"

using namespace std;

int main()

{

string strInput;

cout << "Input a string: " << endl;

cin >> strInput;

string strTemp;

for (size_t i = strInput.length() - 1; i > 1; i--)

{

for (size_t j = 0; j < strInput.length(); j++)

{

if ((i + j) <= strInput.length())

{

size_t szForw = 0;

size_t szBacw = 0;

strTemp = strInput.substr(j, i);

szForw = strInput.find(strTemp);

szBacw = strInput.rfind(strTemp);

if (szBacw != szForw)

{

cout << strTemp << " " << szForw + 1 << endl;

return 0;

}

}

}

}

return 1;

}

3、实现strstr()功能。如主串是12345678,子串是234,则返回2345678。

3、实现strstr()功能。如主串是12345678,子串是234,则返回2345678。

#include

"iostream"

#include "string"

using namespace std;

const char* strstr1(const char*

strMainString, const

char*

strSubString)

{

for

(size_t i = 0;

strMainString[i]; i++)

{

size_t iTempj

= 0;

size_t iTempi =

i;

if

(strMainString[iTempi] == strSubString[iTempj])

{

while(strMainString[iTempi++] ==

strSubString[iTempj++])

{

if

(strSubString[iTempj] == '\0')

return

&strMainString[i];

}

}

}

return NULL;

}

int

main()

{

char str1[]

= "12345678";

char str2[] =

"234";

const char *str3 = strstr1(str1,

str2);

cout << str3 << endl;

return

1;

}

#include "iostream"

#include "string"

using namespace std;

const char* strstr1(const char* strMainString, const char* strSubString)

{

for (size_t i = 0; strMainString[i]; i++)

{

size_t iTempj = 0;

size_t iTempi = i;

if (strMainString[iTempi] == strSubString[iTempj])

{

while(strMainString[iTempi++] == strSubString[iTempj++])

{

if (strSubString[iTempj] == '\0')

return &strMainString[i];

}

}

}

return NULL;

}

int main()

{

char str1[] = "12345678";

char str2[] = "234";

const char *str3 = strstr1(str1, str2);

cout << str3 << endl;

return 1;

}

4、将一句话中的单词倒置,标点符号不倒换。如“i come from tianjin.”,倒换后变成“tianjin. from

come i”。

大体思路:先把整个字符串调整,再针对每个单词进行调整。

示例代码

view sourceprint?

#include "iostream"

#include

"string"

//#include "functional"

//#include

"algorithm"

using namespace std;

int main()

{

cout << "Input a

string: " << endl;

string strOfaLine;

getline(cin, strOfaLine);

size_t

szStrLength = strOfaLine.length();

size_t szTempbeg =

0;

size_t

szTempend = szStrLength - 1;

//第一步,全局交换

while(szTempbeg <

szTempend)

{

swap<char>(strOfaLine[szTempbeg++],

strOfaLine[szTempend--]);

}

//第二步,局部交换

size_t szTempi =

0;

while

(strOfaLine[szTempi])

{

if

(strOfaLine[szTempi] != '

')

{

szTempbeg = szTempi;

while(strOfaLine[szTempi]

!= '\0' &&

strOfaLine[szTempi] != '

')

{

szTempi++;

}

szTempi--;

szTempend = szTempi;

}

while(szTempbeg

< szTempend)

{

swap<char>(strOfaLine[szTempbeg++],

strOfaLine[szTempend--]);

}

szTempi++;

}

cout << strOfaLine <<

endl;

return 1;

}

大体思路:先把整个字符串调整,再针对每个单词进行调整。

示例代码

view sourceprint?

#include "iostream"

#include "string"

//#include "functional"

//#include "algorithm"

using namespace std;

int main()

{

cout << "Input a string: " << endl;

string strOfaLine;

getline(cin, strOfaLine);

size_t szStrLength = strOfaLine.length();

size_t szTempbeg = 0;

size_t szTempend = szStrLength - 1;

//第一步,全局交换

while(szTempbeg < szTempend)

{

swap(strOfaLine[szTempbeg++], strOfaLine[szTempend--]);

}

//第二步,局部交换

size_t szTempi = 0;

while (strOfaLine[szTempi])

{

if (strOfaLine[szTempi] != ' ')

{

szTempbeg = szTempi;

while(strOfaLine[szTempi] != '\0' && strOfaLine[szTempi] != ' ')

{

szTempi++;

}

szTempi--;

szTempend = szTempi;

}

while(szTempbeg < szTempend)

{

swap(strOfaLine[szTempbeg++], strOfaLine[szTempend--]);

}

szTempi++;

}

cout << strOfaLine << endl;

return 1;

}

5、找出字符串中最长最短单词并统计词数

#include "stdafx.h"

#include

#include

#include

using namespace std;

int

main(int

argc, char*

argv[])

{

string line1="We

were her pride of 10 she named us:";

string line2="Benjamin,Phoenix,the Prodigal";

string line3="and

perspicacious pacific suzanne";

string sentence = line1+'

'+line2+'

'+line3;

string separators(" \t;,\v\t\n\r\f");

string word;

vectorlongestWord,shortestWord;

string::size_type

maxLen,minLen,wordLen,count=0;

string::size_type

start_pos=0,end_pos=0;

while

((start_pos=sentence.find_first_not_of(separators,start_pos))!=string::npos)

{

++count;

end_pos =

sentence.find_first_of(separators,start_pos);

if(end_pos==string::npos)

{

wordLen = sentence.size()-start_pos;

}

else

{

wordLen = end_pos-start_pos;

}

word.assign(sentence.begin()+start_pos,sentence.begin()+start_pos+wordLen);

start_pos=sentence.find_first_not_of(separators,end_pos);

if(count==1)

{

minLen=maxLen=wordLen;

longestWord.push_back(word);

shortestWord.push_back(word);

}

else

{

if(wordLen>maxLen)

{

maxLen = wordLen;

longestWord.clear();

longestWord.push_back(word);

}

else if (wordLen

{

minLen = wordLen;

shortestWord.clear();

shortestWord.push_back(word);

}

else if(wordLen==minLen)

{

shortestWord.push_back(word);

}

else if(wordLen==maxLen)

longestWord.push_back(word);

}

}

cout<

count="<

cout<

are:";

for(vector::iterator

i=longestWord.begin();i!=longestWord.end();i++)

{

cout<

}

cout<

for(vector::iterator

j=shortestWord.begin();j!=shortestWord.end();j++)

{

cout<

}

return 0;

}

#include "stdafx.h"

#include

#include

#include

using namespace std;

int main(int argc, char* argv[])

{

string line1="We were her pride of 10 she named us:";

string line2="Benjamin,Phoenix,the Prodigal";

string line3="and perspicacious pacific suzanne";

string sentence = line1+' '+line2+' '+line3;

string separators(" \t;,\v\t\n\r\f");

string word;

vectorlongestWord,shortestWord;

string::size_type maxLen,minLen,wordLen,count=0;

string::size_type start_pos=0,end_pos=0;

while ((start_pos=sentence.find_first_not_of(separators,start_pos))!=string::npos)

{

++count;

end_pos = sentence.find_first_of(separators,start_pos);

if(end_pos==string::npos)

{

wordLen = sentence.size()-start_pos;

}

else

{

wordLen = end_pos-start_pos;

}

word.assign(sentence.begin()+start_pos,sentence.begin()+start_pos+wordLen);

start_pos=sentence.find_first_not_of(separators,end_pos);

if(count==1)

{

minLen=maxLen=wordLen;

longestWord.push_back(word);

shortestWord.push_back(word);

}

else

{

if(wordLen>maxLen)

{

maxLen = wordLen;

longestWord.clear();

longestWord.push_back(word);

}

else if (wordLen<

cout<

for(vector::iterator i=longestWord.begin();i!=longestWord.end();i++)

{

cout<

}

cout<

for(vector::iterator j=shortestWord.begin();j!=shortestWord.end();j++)

{

cout<

}

return 0;

}

#include

#include

#include

#define N 30

char * fun(char * s,int *m) //参数m是通过调试才想到的

{

char chLetter[N]; //用来存放字母

char chNumber[N]; //用来存放数字

int i,j,k;

i=0; //初始化

j=0; //j用来记录字母的个数

k=0; //k用来记录数字的个数

for (i=0; i

{

if (s[i] >= 'A' && s[i] <= 'Z'

//将字母存入chLetter[]

|| s[i] >= 'a' && s[i] <= 'z')

{

chLetter[j]=s[i];

j++;

}

if (s[i] >= '0' && s[i] <='9')

//将数字存入chNumber[]

{

chNumber[k]=s[i];

k++;

}

}

chLetter[j]='';

chNumber[k]='';

*m=j+k; //用来返回最后输入和输出时字符的个数

strcat(chLetter,chNumber);

return chLetter;

}

//主函数

void main()

{

char s[N];

int i;

int m;

char *p;

p=NULL;

printf("请输入字符串(30字符以内):n");

scanf("%s",s);

p=fun(s,&m); //刚开始没定义出这个m来现限制指针p所指数组的长度就出现了后面两个字符乱码

for (i=0; i

{

s[i]=p[i];

}

printf("结果为:");

for (i=0; i

{

printf("%c",s[i]);

}

printf("n");

}

浮点数转换为字符串

#include "stdafx.h"

#include "stdlib.h"

char *F2S(double d, char* str)

{

char str1[40];

int j=0,k,i;

i = (int)d; //浮点数的整数部分

//d = d-(int)d;

while(i>0)

{

str1[j++] = i+'0';

i /= 10;

}

for(k=0;k

str[k] = str1[j-1-k]; //

str[j++] = '.';

d -= (int)d;

for(i=0;i<10;i++)

{

d *= 10;

str[j++] = (int)d+'0';

d -= (int)d;

}

while(str[--j]=='0');

str[++j] = '\0';

return str;

}

int _tmain(int argc, _TCHAR* argv[])

{

double d = 365.897003120000;

char str[20];

char *p = F2S(d, str);

printf("%s\n",str);

printf("%s\n",p);

return 0;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值