字串插入以及字串結束標誌的考察

來源:互聯網
上載者:User

標籤:style   blog   http   color   io   ar   檔案   2014   div   

問題描述:

判斷字元ch是否與str所指字串中的某個字元相同;若相同則什麼也不做,若不同,則將其插入到字串的最後。

範例輸入:

                   abcde

                   f

範例輸出:

                   abcdef

源碼如下:

#include<stdlib.h>#include<conio.h>#include<stdio.h>#include<string.h>void fun (char *str,char ch){    while(*str &&*str!= ch) //字元ch是否與str所指字串中的某個字元不同時什麼也不做        str++;              //指標移動到下一個字元    if(*str==‘\0‘)            //當指標指向字串結束處時執行下列代碼    {        str[0]=ch;            //將現在指標指向的位置處的值‘\0‘替換成ch變數中表示的字元        str[1]=‘\0‘;        //指標的下一個位置重新賦值為字串結束標誌‘\0‘。    }}void main(){    char s[81],c;    system("cls");            //清屏函數,所用標頭檔#include<stdlib.h>    printf("\n Please enter a string:");    gets(s);                //鍵盤接受一個字串    printf("\n Please enter the character to search:");    c=getchar();    fun(s,c);                //函數調用,傳遞地址,其值會改變。    printf("\n The result is %s \n",s);}

程式:

 

字串插入以及字串結束標誌的考察

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.