-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreverseString.c
More file actions
50 lines (39 loc) · 1000 Bytes
/
reverseString.c
File metadata and controls
50 lines (39 loc) · 1000 Bytes
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
39
40
41
42
43
44
45
46
47
48
49
50
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void reverseString(char *string){
int begin = 0;
int end = strlen(string) -1;
char exchangeValue;
while(begin < end){
exchangeValue = string[begin];
string[begin] = string [end];
string[end] = exchangeValue;
begin++;
end --;
}
}
// Função para permitir leitura com espaços em C
char *readLine(FILE* input) {
char *str = NULL;
int counter = 0;
char c;
do {
c = fgetc(input);
if (c != '\r') {
str = realloc(str, sizeof(char) * (counter + 1));
str[counter++] = c;
}
} while (c != '\n' && c != EOF);
str[--counter] = '\0';
return str;
}
int main(){
char *string;
printf("Digite uma string: \n");
string = readLine(stdin);
reverseString(string);
printf("String invertida: %s\n" ,string);
// Desalocando a memória utilizada para alocação dessa string
free(string);
}