[C++面試題]之字串

來源:互聯網
上載者:User
文章目錄
  • 1、怎樣將整數轉換成字串數,並且不用函數 itoa ?
  • 2、已知函數原型是 char *strcpy(char *strDest,const char *strSrc);,其中strDest是目的字串,strSrc是源字串。
  •     (1)不調用C++/C的字串庫函數,請編寫strcpy函數。
  •     (2)strcpy函數把strSrc的內容複寫到strDest,為什麼還要char *類型傳回值?
  • 3、編寫一個函數,作用是把一個char組成的字串迴圈右移n個。比如原來是" abcdefghi “,如果n=2,移位後應該是 “hiabcdefg ”。
  •  
  • 4、將一句話裡的單詞進行倒置,標點符號不倒置。比如一句話:i come from beijing.倒置後變成:beijing. from come i。
  • 5、編程:輸入一行字串,找出其中出現的相同且長度最長的字串,輸出它及其首字元的位置。例如:“yyabcdabjcabceg”,輸出結果應該為 abc和 3。

      基本上求職者進行筆試時沒有不考字串的。字串也是一種相對簡單的資料結構,容易引起面試官反覆發問。事實上,字串也是考驗程式員編程規範和編程習慣的重要考點。不要忽視這些細節,因為這些細節會體現你在作業系統、軟體工程、邊界記憶體處理等方面的知識掌握能力,也會成為企業是否錄用你的參考因素。

1、怎樣將整數轉換成字串數,並且不用函數 itoa ?

    答案:

#include <iostream>using namespace std;int main (){int num =12345,i=0,j=0;char temp[7],str[7];while(num){temp[i]=num%10+'0'; //將整數num從後往前的每一位元轉換成char儲存在temp中i++;num=num/10;}temp[i]=0;cout<<"temp:"<<temp<<endl;i=i-1;//反轉tempwhile (i>=0){str[j++]=temp[i--];}str[j]=0;cout<<"string:"<<str<<endl;return 0;}

 

    如果可以使用 itoa函數的話,則十分簡單,如下:

#include <iostream>#include <stdlib.h>using namespace std;//使用itoa函數int main (){int num=12345;char str[7];itoa(num,str,10);cout<<"integer:"<<num<<endl<<"string:"<<str<<endl;return 0;}

 

2、已知函數原型是 char *strcpy(char *strDest,const char *strSrc);,其中strDest是目的字串,strSrc是源字串。    (1)不調用C++/C的字串庫函數,請編寫strcpy函數。    (2)strcpy函數把strSrc的內容複寫到strDest,為什麼還要char *類型傳回值?

    答案:

(1)代碼如下:

char *strcpy(char *strDest,const char *strSrc){assert((strDest!=NULL)&&(strSrc!=NULL));char *address=strDest;while((*strDest++=*strSrc++)!='\0')NULL;return address;}

(2)為了實現鏈式運算式,返回具體值。

例如:

int length=strlen(strcpy(strDest,"hello world"));

 

3、編寫一個函數,作用是把一個char組成的字串迴圈右移n個。比如原來是" abcdefghi “,如果n=2,移位後應該是 “hiabcdefg ”。

    答案:

(1)使用標準庫函數方法:

void LoopMove(char *pStr,int steps){int n=strlen(pStr)-steps;char temp[MAX_LEN];strcpy(temp,pStr+n);strcpy(temp+steps,pStr);*(temp+strlen(pStr))=‘\0’;strcpy(pStr,temp);}

(2)不使用標準庫函數的方法:

CSDN上一道題(請看第五題和評論)

 4、將一句話裡的單詞進行倒置,標點符號不倒置。比如一句話:i come from beijing.倒置後變成:beijing. from come i。

      解析:解決該問題可以分為兩步:第一步全盤置換該語句成:.gnijieb morf emoc i。第二步進行部分翻轉,如果不是空格,則開始翻轉單詞。

      答案:

具體代碼如下:

#include <iostream>using namespace std;int main (){int num=-12345,i=0,j=0,flag=0,begin,end;char str[]="i come from beijing.";char temp;j=strlen(str)-1;//第一步是進行全盤翻轉while(j>i){temp=str[i];str[i++]=str[j];str[j--]=temp;}//第二步進行部分翻轉i=0;while(str[i]){if(str[i]!=' '){begin=i;while(str[i] && str[i]!=' ')i++;  //找到str[i]為空白格符i=i-1;    //空格符回退一個end=i;}while(end>begin) //部分翻轉{temp=str[begin];str[begin++]=str[end];str[end--]=temp;}i++; }cout<<"string:"<<str<<endl;}

 

5、編程:輸入一行字串,找出其中出現的相同且長度最長的字串,輸出它及其首字元的位置。例如:“yyabcdabjcabceg”,輸出結果應該為 abc和 3。

    答案:

#include <iostream>#include <string>using namespace std;int main (){string str,tep;cout<<"請輸入字串:";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;}

 

    C++面試題已經基本完成了,可能還有一些其它方面的題型,比如作業系統、資料庫、軟體測試、電腦網路等方面的內容。接下來幾天可能會寫一些這方面的內容,我們一起期待吧..

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.