How to reverse a string in C

7 Answers

0 votes
#include <stdio.h>
#include <string.h>
 
#define SIZE 30
   
void reverse(char s[]) {
   int i = 0, len = strlen(s) - 1;
   char rev[SIZE] = "";
      
   while (len >= 0) {
      rev[i++] = s[len--];
   }
   
   rev[i] = '\0';
 
   strcpy(s, rev);
}
   
int main(void) {
    char s[SIZE] = "c c++ java";
 
    reverse(s);
       
    puts(s);
  
    return 0;
}
   
   
   
   
/*
run:
     
avaj ++c c
     
*/

 



answered May 18, 2017 by avibootz
edited Apr 25, 2024 by avibootz
0 votes
#include <stdio.h> 
#include <string.h> 

int main(int argc, char** argv)
{
    char s[6] = "abcde", rev[6] = "";

    strcpy(rev, s);

    _strrev(s); // windows MSVS

    printf("%s\n", rev);
    printf("%s\n", s);

    return 0;
}


/*

run:

abcde
edcba

*/

 



answered May 18, 2017 by avibootz
edited Apr 26, 2024 by avibootz
0 votes
#include <stdio.h>
#include <string.h>
 
#define SIZE 30
   
void reverse(char s[]) {
    int len = strlen(s);
    char tmp;
     
    for (int i = 0, j = len - 1 ; i < (len / 2); i++, j--) {
        tmp = s[j];
        s[j] = s[i];
        s[i] = tmp;
    }
}
   
int main(void) {
    char s[SIZE] = "c c++ java";
 
    reverse(s);
       
    puts(s);
  
    return 0;
}
   
   
   
   
/*
run:
     
avaj ++c c
     
*/

 



answered May 18, 2017 by avibootz
edited Apr 25, 2024 by avibootz
0 votes
#include <stdio.h>
#include <string.h>

int main()
{
    char s[32] = "c c++ java";

    _strrev(s); // windows MSVS

    printf("%s\n", s);

    return 0;
}


/*

run:

avaj ++c c

*/

 



answered May 18, 2017 by avibootz
edited Apr 26, 2024 by avibootz
0 votes
#include <stdio.h>
#include <string.h>
 
#define SIZE 30
   
void reverse(char s[]) {
    char *start = s;
    char *end = start + strlen(s) - 1;
    char ch;
 
    while (end > start) {
        ch = *start;
        *start = *end;
        *end = ch;
        ++start;
        --end;
    }
}
   
int main(void) {
    char s[SIZE] = "c c++ java";
 
    reverse(s);
       
    puts(s);
  
    return 0;
}
   
   
   
   
/*
run:
     
avaj ++c c
     
*/

 



answered May 18, 2017 by avibootz
edited Apr 25, 2024 by avibootz
0 votes
#include <stdio.h>
#include <string.h>

char *strrev(char *s) {
    if (s && *s) {
        char *start = s, *end = s + strlen(s) - 1;
        while (start < end) {
            char t = *start;
            *start++ = *end;
            *end-- = t;
        }
    }
    
    return s;
}

   
int main() 
{ 
    char s[32] = "c c++ java";
    
    strrev(s);
        
    printf("%s\n", s);
      
    return 0;
}

   
    
/*
    
run:
    
avaj ++c c
   
*/

 



answered Apr 25, 2024 by avibootz
0 votes
#include <stdio.h>
#include <string.h>

#define LEN 32

int main(void)
{
    char s[LEN] = "C Programming";
    char* p;

    p = _strrev(s);
    printf("s = %s\n", s);
    printf("p = %s\n", p);

    printf("&s = %p\n", &s[0]);
    printf("&p = %p\n", p);

    return 0;
}



/*
run:

s = gnimmargorP C
p = gnimmargorP C
&s = 000000E58895F898
&p = 000000E58895F898

*/

 



answered Apr 25, 2024 by avibootz

Related questions

1 answer 28 views
2 answers 73 views
1 answer 113 views
1 answer 102 views
1 answer 84 views
84 views asked Jul 29, 2022 by avibootz
1 answer 126 views
126 views asked Sep 3, 2021 by avibootz
3 answers 241 views
241 views asked Jan 16, 2021 by avibootz
...