Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,939 questions

51,876 answers

573 users

How to implement the function strrev() in C

2 Answers

0 votes
#include <stdio.h>
#include <string.h>

char* strrev(char* str) {
    char *p1, *p2;

    if (!str || !*str)
        return str;

    for (p1 = str, p2 = str + strlen(str) - 1; p2 > p1; ++p1, --p2) {
        *p1 ^= *p2;
        *p2 ^= *p1;
        *p1 ^= *p2;
    }

    return str;
}

int main(void) {

    char str[16] = "c c++ java";
    
    strrev(str);

    puts(str);

    return 0;
}



/*
run:

avaj ++c c

*/

 



answered Jan 7, 2024 by avibootz
0 votes
#include <stdio.h>
#include <string.h>

char* strrev(char* str) {
    char *p1, *p2;

    if (!str || !*str)
        return str;

    for (p1 = str, p2 = str + strlen(str) - 1; p2 > p1; ++p1, --p2) {
        char tmp;
        tmp = *p2;
        *p2 = *p1;
        *p1 = tmp;
    }

    return str;
}

int main(void) {

    char str[16] = "c c++ java";
    
    strrev(str);

    puts(str);

    return 0;
}



/*
run:

avaj ++c c

*/

 



answered Jan 7, 2024 by avibootz

Related questions

1 answer 155 views
155 views asked Mar 2, 2021 by avibootz
1 answer 130 views
1 answer 57 views
57 views asked Jun 10, 2025 by avibootz
1 answer 112 views
1 answer 109 views
1 answer 88 views
88 views asked Aug 2, 2024 by avibootz
1 answer 110 views
...