Twitter iPhone pliant OnePlus 11 PS5 Disney+ Orange Livebox Windows 11

strsub

31 réponses
Avatar
Jean Pierre Daviau
Bonjour à tous,

N'ayant pu trouver une fonction du genre sur google...

La fonction imprime null

/* strsub.c */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char * strsub(char *, int);

char * strsub(char * source, int end){

char *dest = (char*)malloc(strlen(source)+1);
char *tmp = source + end;
strcpy(dest, "a");

if(strlen(source)<255 && strlen(source) > (unsigned)source +
end){

while(source++ < tmp){
*dest++ = *source;
}
dest = '\0';
return dest;
}else{
return "Chaîne de plus de 255 lettres ou indice de fin trop
grand.";
}
}

int main(void)
{
char *chaine = "Chaîne de plus de 255 lettres.";

printf("--%s--\n", strsub(chaine + 4, 8));
return 0;
}


--
Merci

Jean Pierre Daviau
--
windows Xp
asus p4 s533/333/133
Intel(R) Celeron (R) CPU 2.00 GHz
Processor Radeon7000 0x5159 agp

1 réponse

1 2 3 4
Avatar
j4e8a16n
On Feb 1, 6:53 am, Vincent Lefevre <vincent+ wrote:
Dans l'article <47a1fe23$0$12987$,
  Antoine Leca écrit:





#include <string.h>
#include <stdlib.h>
#include "strlcpy.h"
/*size_t strlcpy(char *dst, const char *src, size_t siz);*/
char* strsub(const char chaîne[], unsigned début, unsigned fin)
{
size_t alloué;
char *r;
  if( début>strlen(chaîne)
   || fin<début
   || !(r = malloc(alloué = fin-début+1)) )
     return NULL;
  strlcpy(r, chaîne+début, alloué);
  return r;
}
S'il vous manque strlcpy sur votre machine,
ftp://ftp.openbsd.org/pub/OpenBSD/src/lib/libc/string/strlcpy.c
(et récupérez donc en même temps
ftp://ftp.openbsd.org/pub/OpenBSD/src/lib/libc/string/strlcat.c, et jette z
donc un oeil distrait à l'indicatif utilisateur de la dernière perso nne ayant
modifié ce source... ;-) )


Et récupérez un compilateur qui reconnaisse les caractères accentu és. :)

--
Vincent Lefèvre - Web: <http://www.vinc17.org/>
100% accessible validated (X)HTML - Blog: <http://www.vinc17.org/blog/>
Work: CR INRIA - computer arithmetic / Arenaire project (LIP, ENS-Lyon)- H ide quoted text -

- Show quoted text -


J'allais le dire. . .


1 2 3 4