"Christmas - the time to fix the computers of your loved ones" « Lord Wyrm

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

Ecraft 26.11.2003 - 15:03 5116 41
Posts

Ecraft

Here to stay
Registered: Mar 2002
Location:
Posts: 1096
char* s1 = "wasweisich";

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

semteX

Risen from the banned
Avatar
Registered: Oct 2002
Location: Pre
Posts: 14362
edt: geht doch ned
Bearbeitet von semteX am 26.11.2003, 15:11

funka

Legend
ex-prophet(down below)
Registered: Sep 2000
Location: Vienna / SF
Posts: 6131
afair

#include <string.h>

size_t strlen(const char *s);

Ecraft

Here to stay
Registered: Mar 2002
Location:
Posts: 1096
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
Bearbeitet von Ecraft am 26.11.2003, 15:18

funka

Legend
ex-prophet(down below)
Registered: Sep 2000
Location: Vienna / SF
Posts: 6131
dann mach eine whileschleife die bis zum \0 mitzaehlt steck das ganze in eine funktion und nenns strlen_bydummies()

b_d

pixel imperfect
Avatar
Registered: Jul 2002
Location: 0x3FC
Posts: 10490
EDIT: ups, angabe falsch gelesen ;)

sry

MFG

-ICH-
Bearbeitet von b_d am 26.11.2003, 15:19

funka

Legend
ex-prophet(down below)
Registered: Sep 2000
Location: Vienna / SF
Posts: 6131
semtex und braindeath
beide in die ecke
hop

;)

wenn sogar funk mehr c knowledge hat wirds peinlich

Ecraft

Here to stay
Registered: Mar 2002
Location:
Posts: 1096
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

Risen from the banned
Avatar
Registered: Oct 2002
Location: Pre
Posts: 14362
:bash: also hätts doch gestimmt naaa :(

Ringding

Pilot
Avatar
Registered: Jan 2002
Location: Perchtoldsdorf/W..
Posts: 4300
Ein bissi kürzer :)

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

fresserettich

Here to stay
Registered: Jul 2002
Location: hier
Posts: 5321
int strlen (char str[])
{
int i=0;

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

wäre auch ne möglichkeit

FMFlash

tranceCoder
Avatar
Registered: Mar 2001
Location: Wien
Posts: 2037
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

STIEGL Case 0wn0r
Avatar
Registered: Nov 2002
Location: Braunau
Posts: 1016
g0t assembler ??

Ringding

Pilot
Avatar
Registered: Jan 2002
Location: Perchtoldsdorf/W..
Posts: 4300
Von dir oder abgeschrieben?

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

Jedi

PROGrAMmER
Avatar
Registered: May 2002
Location: linz
Posts: 1871
### ups... ###
Bearbeitet von Jedi am 26.11.2003, 17:39
Kontakt | Unser Forum | Über overclockers.at | Impressum | Datenschutz