Thread: Mid function in C

  1. #1
    Registered User
    Join Date
    Nov 2008
    Location
    Santa Catarina - Brasil
    Posts
    184

    Mid function in C

    Code:
    char *substr(const char *pstr, int start, int numchars) 
    {
    	char *pnew = malloc(numchars+1);
    	
    	strncpy(pnew, pstr + start, numchars);
    	pnew[numchars] = '\0';
    	return pnew;
    }
    
    int strrpos (unsigned char *pStrText, char pChar){	// Última ocorrência do caractere.
    	
    	int iLen, iCont;
    	iLen = strlen (pStrText);
    
    	if (iLen > 0){
    		for (iCont=iLen - 1; iCont>=0; iCont--){
    			if (pStrText[iCont] ==  pChar)
    				return iCont;
    		}
    		return 0;	// Não houve ocorrências
    	}
    	else { return 0; }	// String Nula
    }
    
    int ProcessaChamado (char *pPacote, int pLen){
    	
    	char *cTeste = malloc (30);
    	char *cFuncao;
    	char *cPacote = malloc(pLen);
    	
    	int iPosStart, iPosEnd, iPosTam;
    			
    	//Separa o Pacote
    	iPosStart = strrpos (pPacote, cSTX);
    	if(iPosStart >= 0) 
    		iPosStart++;
    	
    	iPosEnd = strrpos (pPacote, cCR);
    	
    	iPosTam = iPosEnd - iPosStart;
    		
    	if((iPosStart) && (iPosEnd)){
    		cPacote = substr(pPacote, iPosStart, iPosTam);
    	} 
    	else { return 0; }
    	
    	if(strlen (cPacote) > 0){
    		
    		cFuncao = substr (cPacote, 0, 2);
    		
    		sprintf (cTeste, "%s %s %c %c %d", cFuncao, cPacote, cFuncao[0], cFuncao[1], strlen(cFuncao));
    		printCom1 ("TESTE: %s\n", cTeste);	
    		
    		if (strcmp (cFuncao, "OP") == 0)
    			printCom1 ("OK!");
    		
    	} 
    	else { return 0; }
    	
    	return 1;
    }
    I need got the XX (pos 1 e 2 of string) in send string

    Send: XX34567890AB

    Received: X3 X34567890AB X 3 2

  2. #2
    Resu Deretsiger Nightowl's Avatar
    Join Date
    Nov 2008
    Location
    /dev/null
    Posts
    186
    Umm . . . alright. I don't fully understand what you mean.

    Could you try to post some more information on what you're trying to do here?

  3. #3
    Registered User
    Join Date
    Nov 2008
    Location
    Santa Catarina - Brasil
    Posts
    184
    the substr function, is similliar to function MID to get char at a position in a string.

    In function ProcessaChamado , I Send [2]XX34567890AB[13][10], and your length

    when I call cFuncao = substr (cPacote, 0, 2);

    I Need Get XX of sended package.

    Obs: cPacote is XX34567890AB without 2 13 10 char.

  4. #4
    Resu Deretsiger Nightowl's Avatar
    Join Date
    Nov 2008
    Location
    /dev/null
    Posts
    186
    I'm trying to understand what you mean. Tell me if I'm understanding you correctly.

    The substr() function returns a small section of a string in a new string, standard stuff.

    In the function ProcessaChamado, you pass it a string and a length . . . according to the prototype.

    What exactly, do you mean by "I Need Get XX of sended package"? Do you mean that you need to get the length of the original string? Or get the first two characters?

  5. #5
    Registered User
    Join Date
    Nov 2008
    Location
    Santa Catarina - Brasil
    Posts
    184
    get the first two characters

    sorry for my english guy

    im brazilian!

  6. #6
    Resu Deretsiger Nightowl's Avatar
    Join Date
    Nov 2008
    Location
    /dev/null
    Posts
    186
    Oh, that's it . . . sorry. No, just remember that you should elaborate on what you mean, and we'll understand you much better.

    Code:
     . . .
    if(strlen (cPacote) > 0) {	
        cFuncao = substr (cPacote, 0, 2);
     . . .
    Are you not getting the first two characters there? (BTW, you should check if the string is longer than 2 chars, not 1. Otherwise you could end up trying to get the first two characters of a 1-character string.)

  7. #7
    Registered User
    Join Date
    Nov 2008
    Location
    Santa Catarina - Brasil
    Posts
    184
    Sorry I don't undestand,
    I'm confused

  8. #8
    Resu Deretsiger Nightowl's Avatar
    Join Date
    Nov 2008
    Location
    /dev/null
    Posts
    186
    Does that code segment not give you the first two characters in the string cPacote?

  9. #9
    Registered User
    Join Date
    Nov 2008
    Location
    Santa Catarina - Brasil
    Posts
    184
    yes, segment give second and third characters in the string cPacote.

  10. #10
    Resu Deretsiger Nightowl's Avatar
    Join Date
    Nov 2008
    Location
    /dev/null
    Posts
    186
    . . . so just use something similar for pPacote?

  11. #11
    Registered User
    Join Date
    Nov 2008
    Location
    Santa Catarina - Brasil
    Posts
    184
    pPacote is [2]XX34567890AB[13][10] is ok!

    cPacote is X34567890AB

    But Im Call Function in cPacote = substr(pPacote, iPosStart, iPosTam);

    Where iPosStart = 1 iPosTam = 11

    And corretly return of cPacote need be XX34567890AB
    and not [2]X34567890AB

  12. #12
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Are you saying that the return value of substr() is not as you expect? It seems okay to me; it extracts a string starting at the given position with the given length. For example, "Hello" starting at 1 with length 2 would be "el".

    Note that the last parameter to substr is a length, not an index.

    I too am not sure what you are asking. What is "[2]X34567890AB"?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  13. #13
    Registered User
    Join Date
    Nov 2008
    Location
    Santa Catarina - Brasil
    Posts
    184
    [2]X34567890AB is the returned string of substr function.

    but,

    I Need this XX34567890AB

  14. #14
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    If I got it then a lot depends on what strrpos() looks like and the arguments you are passing to it cCR and cSTX. So if you could tell some more about it, that would be helpful.

  15. #15
    Registered User
    Join Date
    Nov 2008
    Location
    Santa Catarina - Brasil
    Posts
    184
    iPosStart = strrpos (pPacote, cSTX);

    if(iPosStart >= 0)
    iPosStart++;

    iPosEnd = strrpos (pPacote, cCR);

    iPosTam = iPosEnd - iPosStart;

    Result:

    cPacote: XX34567890AB
    pPacote: X34567890AB
    iPosStart/iPosEnd/iPosTam: 1 12 11

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM