Publi

Volteando cadenas

En C, una de las pequeñas cosas que a veces nos hace más lentos a la hora de hacer un pequeño programa es la posibilidad de darle la vuelta a una cadena. Bien, aquí traigo un pequeño código (función y ejemplo) de strrev, que además de poder dar la vuelta a una cadena, puede manipular sólo ciertos caracteres:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <stdio.h>
#include <string.h>

char *strrev(char *str, int strl)
{
    /* Creamos punteros al principio y al final de la cadena */
    /*       C   A   R   A   C   T   E   R   5   \0 */
    /*       |                               |      */
    char *start = str, *end = str + strl - 1;
    char temp;
    /* Iteramos hasta que el principio y el fin coincidan */
    while(start < end)
        {
            /* Intercambiamos los caracteres */
            /* 1º  C A R A C T E R 5 */
            /* 2º  5 A R A C T E R C */
            /* 3º  5 R R A C T E A C */
            /* 4º  5 R E A C T R A C */
            /* 4º  5 R E T C A R A C */
            temp = *start;
            *start++ = *end;
            *end-- = temp;
        }
    return str;
}

/* Ejemplo */
int main()
{
    char cadena[50]="Una cadena de caracteres cualquiera";
    char tmp[50]="Una cadena de caracteres cualquiera";
    char *cad2=cadena;
    printf("Cadena original: %s\n", cadena);
    printf("Cadena al revés: %s\n", strrev(tmp, strlen(tmp)));
    strrev(cadena+14, 10);
    printf("Caracteres al revés: %s\n", cad2);
    return 0;
}

Esta función la encontré hace mucho tiempo navegando y me pareció muy interesante, sobre todo por cómo está hecha, creo que es de las más rápidas.

También podría interesarte....

There are 5 comments left Ir a comentario

  1. Pingback: Poesía binaria » Cambio de base (de base b1 a b2… hasta base36 o tal vez más) /

  2. Kelsey Radley /
    Usando Google Chrome Google Chrome 117.0.0.0 en Windows Windows NT

    Great information! I will visit your site more often so I am updated. The ideas I gather from here cannot be paid with money. Thanks very much! Bitlife

  3. mehmood alam /
    Usando Google Chrome Google Chrome 120.0.0.0 en Windows Windows NT

    I merely imagined it usually is a perception to write could possibly help anybody ended up being experiencing difficulity looking into nevertheless We are somewhat doubtful only are permitted to placed labels along with details in below. river monster casino

  4. meh /
    Usando Google Chrome Google Chrome 120.0.0.0 en Windows Windows NT

    I have read your blog it is very helpful for me. I want to say thanks to you. I have bookmark your site for future updates. orion stars login

  5. yitzchak kerrigan /
    Usando Google Chrome Google Chrome 121.0.0.0 en Windows Windows NT

    Excellent Submit, I will be a huge believer inside submitting feedback about web sites to be able to allow the website copy writers understand that they’ve extra one thing useful to be able to the net! Order Tapentadol

Leave a Reply