FMUSER Wirless Transmit Video And Audio More Easier !

[email protected] WhatsApp +8618078869184
Language

    Some common C language string operations

     

    "# string output in reverse order Realize the logic, obtain the string length through strlen, and then perform cross assignment through len / 2. It should be noted here that it is not necessary to consider whether len is odd or even. If len is an odd number, the last character does not need to be in reverse order. If it is an even number, the last two characters are in reverse order. #include""stdio.h"" voidrechange_ str(char*str) { inti,len; chartmp; if(NULL==str){ return; } len=strlen(str); for(i=0; i< len/2; i ++) { tmp = str[i]; str[i] = str[len-i-1]; str[len-i-1] = tmp; } } int main(void) { char str[20] = "" hello,world""; printf(""%s "",str); rechange_ str(str); printf(""%s "",str); return(0); } Program output hello,world dlrow,olleh -------------------------------- Processexitedafter0.02841secondswithreturnvalue0 Please press any key to continue #Integer to string Implementation logic. Each integer can be converted to hexadecimal. From one bit to ten bits, the hundred bits can be obtained through the% operation plus / operation, and then a character array is used to save 0-f. Use the corresponding value of a single digit to convert into characters. Note that the converted string is reverse. Also consider how to deal with the incoming negative number, and then flip the string to complete the last whole operation The following code needs to be studied. You'd better run it yourself. #include""stdio.h"" char*sky_ itoa(intvalue,char*str,unsignedintradix) { charlist[]=""0123456789ABCDEF""; unsignedinttmp_ value; inti,j,k; if(NULL==str){ returnNULL; } if(2!= radix&&8!= radix&&10!= radix&&16!= radix){ returnNULL; } i=0; k=0; if(radix==10&&value< 0) { tmp_ value = ( unsigned int)(0 - value); str[i++] = '-'; k=1; }else{ tmp_ value=(unsignedint)value; } do{ str[i++]=list[tmp_ value%radix]; tmp_ value/=radix; }while(tmp_ value); str[i]=''; //Flip chartmp; for(j=k; j< ( i+k)/2; j++) { tmp = str[j]; str[j] = str[i+k-j-1]; str[i+k-j-1] = tmp; } returnstr; } intmain(void) { inta=1254545; charstr[100]={0}; printf(""%s "",sky_ itoa(a,str,2)); printf(""%s "",sky_ itoa(a,str,8)); printf(""%s "",sky_ itoa(a,str,10)); printf(""%s "",sky_ itoa(a,str,16)); return(0); } Program output 100110010010010010001 four million six hundred and twenty-two thousand two hundred and twenty-one one million two hundred and fifty-four thousand five hundred and forty-five one hundred and thirty-two thousand four hundred and ninety-one -------------------------------- Processexitedafter0.02963secondswithreturnvalue0 Please press any key to continue #String copy Implement logic and assign values one by one until '' stops #include""stdio.h"" char*sky_ strcpy(char*dst,constchar*str) { if(NULL==dst||NULL==str){ returnNULL; } char*ret=dst; while(*str!=''){ *dst++=*str++; } returnret; } intmain(void) { charstr_ 1[100]=""hello,world""; charstr[100]={0}; sky_ strcpy(str,str_ 1); printf(""str_ 1:%s "",str_ 1); printf(""str:%s "",str); return(0); } Program output str_ 1:hello,world str:hello,world -------------------------------- Processexitedafter0.03334secondswithreturnvalue0 Please press any key to continue #String comparison 1. If the normal comparison is the same, implement the logic to judge whether the string length is the same. If it is the same, compare the characters one by one #include""stdio.h"" intsky_ strcmp(char*dst,char*str) { inti,len; if(NULL==dst||NULL==str){ return0; } if(strlen(dst)!= strlen(str)){ return0; } len=strlen(dst); for(i=0; i< len; i++) { if(*dst++!=* str++){ return0; } } return1; } intmain(void) { charstr_ 1[100]=""hello,world""; charstr_ 2[100]=""hello,world""; charstr[100]=""adfs""; printf(""%d "",sky_ strcmp(str_ 1,str)); printf(""%d "",sky_ strcmp(str_ 1,str_ 2)); return(0); } Program output 0 one -------------------------------- Processexitedafter0.02802secondswithreturnvalue0 Please press any key to continue 2. Ignore case string comparison Implement logic. When comparing characters, they can be uniformly converted to uppercase or lowercase, and then compared. It is no different from normal comparison #include""stdio.h"" #defineCONVERT(c)(((c)>='A'&&(c)<= ' Z') ? (( c) - ' A' + ' a') : ( c)) intsky_ strcmp(char*dst,char*str) { inti,len; if(NULL==dst||NULL==str){ return0; } if(strlen(dst)!= strlen(str)){ return0; } len=strlen(dst); for(i=0; i< len; i++) { if(CONVERT(*dst)!= CONVERT(*str)){ return0; } dst++; str++; } return1; } intmain(void) { charstr_ 1[100]=""heLlo,world""; charstr_ 2[100]=""hello,world""; charstr[100]=""adfs""; printf(""%d "",sky_ strcmp(str_ 1,str)); printf(""%d "",sky_ strcmp(str_ 1,str_ 2)); return(0); } Program output 0 one -------------------------------- Processexitedafter0.04624secondswithreturnvalue0 Please press any key to continue #Memcpy function implementation The implementation logic is mainly completed by assigning values one by one 1. Copy coverage is not considered #include""stdio.h"" #include""string.h"" void*sky_ memecpy(void*dst,constvoid*str,intn) { if(NULL==dst||NULL==str||n<= 0) { returnNULL; } char*pdst=(char*)dst; char*pstr=(char*)str; while(n--){ *pdst++=*pstr++; } returndst; } intmain(void) { charstr_ 1[100]=""heLlo,world""; charstr_ 2[100]=""sdfsdfs""; sky_ memecpy(str_ 2,str_ 1,strlen(str_ 1)); printf(""%s "",str_ 2); return(0); } Program output heLlo,world -------------------------------- Processexitedafter0.02516secondswithreturnvalue0 Please press any key to continue 2. Consider copy coverage Copy overwrite is a problem we need to pay attention to when copying strings. In my following example program, there is a problem when using the first function, and there is no problem when using the second function. The reason is that the addresses of our source string and destination string are the same. We want to move the string back one position, but there is actually a problem. #include""stdio.h"" #include""string.h"" void*sky_ memecpy_ 1(void*dst,constvoid*str,intn) { if(NULL==dst||NULL==str||n<= 0) { returnNULL; } char*pdst=(char*)dst; char*pstr=(char*)str; while(n--){ *pdst++=*pstr++; } returndst; } void*sky_ memecpy(void*dst,constvoid*str,intn) { if(NULL==dst||NULL==str||n<= 0) { returnNULL; } char*pdst=(char*)dst; char*pstr=(char*)str; if(pdst>pstr&&pdst< pstr + n) { pdst = pdst + n - 1; pstr = pstr + n - 1; while(n--){ *pdst--=*pstr--; } }else{ while(n--){ *pdst++=*pstr++; } } returndst; } intmain(void) { charstr_ 1[100]=""heLlo,world""; charstr_ 2[100]=""heLlo,world""; sky_ memecpy_ 1(str_ 1+1,str_ 1,strlen(str_ 1)); printf(""%s "",str_ 1); sky_ memecpy(str_ 2+1,str_ 2,strlen(str_ 2)); printf(""%s "",str_ 2); return(0); } Program output hhhhhhhhhhhh hheLlo,world -------------------

     

     

     

     

    List all Question

    Nickname

    Email

    Questions

    Our other product:

    Professional FM Radio Station Equipment Package

     



     

    Hotel IPTV Solution

     


      Enter email  to get a surprise

      fmuser.org

      es.fmuser.org
      it.fmuser.org
      fr.fmuser.org
      de.fmuser.org
      af.fmuser.org ->Afrikaans
      sq.fmuser.org ->Albanian
      ar.fmuser.org ->Arabic
      hy.fmuser.org ->Armenian
      az.fmuser.org ->Azerbaijani
      eu.fmuser.org ->Basque
      be.fmuser.org ->Belarusian
      bg.fmuser.org ->Bulgarian
      ca.fmuser.org ->Catalan
      zh-CN.fmuser.org ->Chinese (Simplified)
      zh-TW.fmuser.org ->Chinese (Traditional)
      hr.fmuser.org ->Croatian
      cs.fmuser.org ->Czech
      da.fmuser.org ->Danish
      nl.fmuser.org ->Dutch
      et.fmuser.org ->Estonian
      tl.fmuser.org ->Filipino
      fi.fmuser.org ->Finnish
      fr.fmuser.org ->French
      gl.fmuser.org ->Galician
      ka.fmuser.org ->Georgian
      de.fmuser.org ->German
      el.fmuser.org ->Greek
      ht.fmuser.org ->Haitian Creole
      iw.fmuser.org ->Hebrew
      hi.fmuser.org ->Hindi
      hu.fmuser.org ->Hungarian
      is.fmuser.org ->Icelandic
      id.fmuser.org ->Indonesian
      ga.fmuser.org ->Irish
      it.fmuser.org ->Italian
      ja.fmuser.org ->Japanese
      ko.fmuser.org ->Korean
      lv.fmuser.org ->Latvian
      lt.fmuser.org ->Lithuanian
      mk.fmuser.org ->Macedonian
      ms.fmuser.org ->Malay
      mt.fmuser.org ->Maltese
      no.fmuser.org ->Norwegian
      fa.fmuser.org ->Persian
      pl.fmuser.org ->Polish
      pt.fmuser.org ->Portuguese
      ro.fmuser.org ->Romanian
      ru.fmuser.org ->Russian
      sr.fmuser.org ->Serbian
      sk.fmuser.org ->Slovak
      sl.fmuser.org ->Slovenian
      es.fmuser.org ->Spanish
      sw.fmuser.org ->Swahili
      sv.fmuser.org ->Swedish
      th.fmuser.org ->Thai
      tr.fmuser.org ->Turkish
      uk.fmuser.org ->Ukrainian
      ur.fmuser.org ->Urdu
      vi.fmuser.org ->Vietnamese
      cy.fmuser.org ->Welsh
      yi.fmuser.org ->Yiddish

       
  •  

    FMUSER Wirless Transmit Video And Audio More Easier !

  • Contact

    Address:
    No.305 Room HuiLan Building No.273 Huanpu Road Guangzhou China 510620

    E-mail:
    [email protected]

    Tel / WhatApps:
    +8618078869184

  • Categories

  • Newsletter

    FIRST OR FULL NAME

    E-mail

  • paypal solution  Western UnionBank OF China
    E-mail:[email protected]   WhatsApp:+8618078869184   Skype:sky198710021 Chat with me
    Copyright 2006-2020 Powered By www.fmuser.org

    Contact Us