C++: Länge eines Strings an einem char Pointer?

Seite 1 von 3 - Forum: Coding Stuff auf overclockers.at

URL: https://www.overclockers.at/coding-stuff/c_laenge_eines_strings_an_einem_char_pointer_99683/page_1 - zur Vollversion wechseln!


Ecraft schrieb am 26.11.2003 um 15:03

char* s1 = "wasweisich";

Gibts hier irgendeinen schmäh wie man die Länge dieses Strings herausfindet ohne die Klassenbibliothek zu verwenden?


semteX schrieb am 26.11.2003 um 15:04

edt: geht doch ned


funka schrieb am 26.11.2003 um 15:13

afair

#include <string.h>

size_t strlen(const char *s);


Ecraft schrieb am 26.11.2003 um 15:14

Zitat von funka
afair

#include <string.h>

size_t strlen(const char *s);

schön wärs... unsere praktikum tussi will das nicht!!!! Der wär am liebsten das Rad neu zu erfinden! :D


funka schrieb am 26.11.2003 um 15:15

dann mach eine whileschleife die bis zum \0 mitzaehlt steck das ganze in eine funktion und nenns strlen_bydummies()


b_d schrieb am 26.11.2003 um 15:15

EDIT: ups, angabe falsch gelesen ;)

sry

MFG

-ICH-


funka schrieb am 26.11.2003 um 15:20

semtex und braindeath
beide in die ecke
hop

;)

wenn sogar funk mehr c knowledge hat wirds peinlich


Ecraft schrieb am 26.11.2003 um 15:23

soda für die nachwelt, habs hingebracht:

int length_for_dummies(const char* t)
{
int k=0;
while(*(t+k)!='\0') k++;
return k;
}

Danke an alle!


semteX schrieb am 26.11.2003 um 16:03

:bash: also hätts doch gestimmt naaa :(


Ringding schrieb am 26.11.2003 um 16:14

Ein bissi kürzer :)

Code:
int length_for_dummies(const char* t)
{
  int k=0;
  while(*t++) k++;
  return k;
}


fresserettich schrieb am 26.11.2003 um 16:22

int strlen (char str[])
{
int i=0;

while (str[i] != '\0')
{
i++;
} /* while */
return (i);
}//strlen

wäre auch ne möglichkeit


FMFlash schrieb am 26.11.2003 um 17:27

Zitat von Ecraft
schön wärs... unsere praktikum tussi will das nicht!!!! Der wär am liebsten das Rad neu zu erfinden! :D

dann schlag zurück und präsentier ihr die optimierte variante ;)


Code:
        CODESEG

        public  strlen

strlen  proc

        .FPO    ( 0, 1, 0, 0, 0, 0 )

string  equ     [esp + 4]

        mov     ecx,string              ; ecx -> string
        test    ecx,3                   ; test if string is aligned on 32 bits
        je      short main_loop

str_misaligned:
        ; simple byte loop until string is aligned
        mov     al,byte ptr [ecx]
        inc     ecx
        test    al,al
        je      short byte_3
        test    ecx,3
        jne     short str_misaligned

        add     eax,dword ptr 0         ; 5 byte nop to align label below

        align   16                      ; should be redundant

main_loop:
        mov     eax,dword ptr [ecx]     ; read 4 bytes
        mov     edx,7efefeffh
        add     edx,eax
        xor     eax,-1
        xor     eax,edx
        add     ecx,4
        test    eax,81010100h
        je      short main_loop
        ; found zero byte in the loop
        mov     eax,[ecx - 4]
        test    al,al                   ; is it byte 0
        je      short byte_0
        test    ah,ah                   ; is it byte 1
        je      short byte_1
        test    eax,00ff0000h           ; is it byte 2
        je      short byte_2
        test    eax,0ff000000h          ; is it byte 3
        je      short byte_3
        jmp     short main_loop         ; taken if bits 24-30 are clear and bit
                                        ; 31 is set

byte_3:
        lea     eax,[ecx - 1]
        mov     ecx,string
        sub     eax,ecx
        ret
byte_2:
        lea     eax,[ecx - 2]
        mov     ecx,string
        sub     eax,ecx
        ret
byte_1:
        lea     eax,[ecx - 3]
        mov     ecx,string
        sub     eax,ecx
        ret
byte_0:
        lea     eax,[ecx - 4]
        mov     ecx,string
        sub     eax,ecx
        ret

strlen  endp

        end


chaze schrieb am 26.11.2003 um 17:30

g0t assembler ??


Ringding schrieb am 26.11.2003 um 17:31

Von dir oder abgeschrieben?

Schaut irgendwie aus, als käme es aus der MS CRT.


Jedi schrieb am 26.11.2003 um 17:36

### ups... ###




overclockers.at v4.thecommunity
© all rights reserved by overclockers.at 2000-2025