Foro de elhacker.net

Programación => Programación C/C++ => Mensaje iniciado por: el_nene_kpy en 23 Febrero 2012, 19:44 pm



Título: AYUDA Eliminar tabulaciones y \t de cadenas en ANSI C
Publicado por: el_nene_kpy en 23 Febrero 2012, 19:44 pm
Buenas,

tengo que hacer uso de la funcion strtok (en ansi C) para eliminar tabulaciones y \t's incluidos en mitad de una cadena leida de fichero, como muestro en el siguiente ejemplo:
Citar
Hola\ta todos       que\tal estais?

pero no tengo ni la mas remota idea de que segundo argumento debo meter. Me es urgente, por favor.

Muchas gracias


Título: Re: AYUDA Eliminar tabulaciones y \t de cadenas en ANSI C
Publicado por: naderST en 23 Febrero 2012, 23:53 pm
Cita de: cplusplus.com
Código
  1. char * strtok ( char * str, const char * delimiters );
Split string into tokens

A sequence of calls to this function split str into tokens, which are sequences of contiguous characters separated by any of the characters that are part of delimiters.

On a first call, the function expects a C string as argument for str, whose first character is used as the starting location to scan for tokens. In subsequent calls, the function expects a null pointer and uses the position right after the end of last token as the new starting location for scanning.

To determine the beginning and the end of a token, the function first scans from the starting location for the first character not contained in delimiters (which becomes the beginning of the token). And then scans starting from this beginning of the token for the first character contained in delimiters, which becomes the end of the token.

This end of the token is automatically replaced by a null-character by the function, and the beginning of the token is returned by the function.

Once the terminating null character of str has been found in a call to strtok, all subsequent calls to this function with a null pointer as the first argument return a null pointer.

Para mas información revisa este enlace:
http://www.cplusplus.com/reference/clibrary/cstring/strtok/ (http://www.cplusplus.com/reference/clibrary/cstring/strtok/)

Creo que esto es lo que necesitas:
Código
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. int main()
  5. {
  6.    char cadena[] = "Hola\ta todos       que\tal estais?";
  7.    char * ptr_token;
  8.  
  9.    ptr_token = strtok(cadena,"\t ");
  10.  
  11.    while(ptr_token)
  12.    {
  13.        printf("%s\n",ptr_token);
  14.        ptr_token = strtok(NULL,"\t ");
  15.    }
  16.  
  17.    return 0;
  18. }
  19.  
  20.  


Título: Re: AYUDA Eliminar tabulaciones y \t de cadenas en ANSI C
Publicado por: d91 en 13 Marzo 2014, 03:45 am
este codigo si funciona....
#include <stdio.h>
#include <string.h>
#include <conio.h>
#include<ctype.h>
#include<iostream.h>
#include<io.h>

int main()
{
    char cadena[] = "Hola\ta todos que\ttal estais?";
    char *ptr_token;
    int lon, i, num=0;
    lon=strlen(cadena);
     printf("la cadena con tabs: %s",cadena);
    for(i=0; i<lon; i++)
       if(cadena [ i ] =='\t')
      cadena [ i ] =' ';
           printf("\nlo que tiene ahora la cadena: %s",cadena);
    getch();
}


Título: Re: AYUDA Eliminar tabulaciones y \t de cadenas en ANSI C
Publicado por: leosansan en 13 Marzo 2014, 05:30 am
Para cuando postees el código elige las etiquetas GeSHi y toma la C++ y en medio de las etiquetas Code que aparecen "pegas" tu código. Al no hacerlo de esta manera parte del código no sale correcta, especialmente las matrices.

Supongo que lo que querías poner era simplemente:


Código
  1. #include <stdio.h>
  2. #include <conio.h>
  3.  
  4. int main()
  5. {
  6.    char cadena[] = "Hola\ta todos        que\ttal estais?";
  7.    int  i;
  8.    printf("la cadena con tabs: %s",cadena);
  9.    for(i=0; cadena [i]; i++)
  10.    if(cadena [i] =='\t')
  11.      cadena [i] =' ';
  12.    printf("\nlo que tiene ahora la cadena: %s",cadena);
  13.    getch();
  14.    return 0;
  15. }

Y la salida para tu código es:

Citar

la cadena con tabs: Hola        a todos        que      tal estais?
lo que tiene ahora la cadena: Hola a todos        que tal estais?



Mientras que la salid del código de naderST:

Citar

Hola a todos que tal estais?
Process returned 0 (0x0)  


Una sutil diferencia.

¡¡¡¡ Saluditos! ..... !!!!


(http://st.forocoches.com/foro/images/smilies/aaaaa.gif)