How to reverse a string length without string functions in C

2 Answers

0 votes
#include <stdio.h> 

void _reverse(char *s, int len) 
{ 
    int i = 0, end = len - 1, temp; 
    while (i < end) { 
        temp = s[i]; 
        s[i] = s[end]; 
        s[end] = temp; 
        i++; end--; 
    } 
} 

int main(int argc, char **argv)
{ 
	char s[10] = "abcde"; 
	
    _reverse(s, 5);
    printf("%s\n", s); 
     
    return 0; 
}   
 
 
/*
run:
 
edcba
 
*/

 



answered Jan 14, 2019 by avibootz
0 votes
#include <stdio.h> 

int _strlen(char *s) {  
    int i = 0;
	
	while (s[i++] != '\0');
        
	return i - 1; 
} 

void _reverse(char *s)  { 
    int i = 0, end = _strlen(s) - 1, temp; 
    while (i < end) { 
        temp = s[i]; 
        s[i] = s[end]; 
        s[end] = temp; 
        i++; end--; 
    } 
} 

int main(int argc, char **argv)
{ 
	char s[10] = "abcde"; 
	
    _reverse(s);
	
	printf("%s\n", s); 
     
    return 0; 
}   
 
 
/*
run:
 
edcba
 
*/

 



answered Jan 14, 2019 by avibootz
...