-
Notifications
You must be signed in to change notification settings - Fork 0
/
4a.c
56 lines (40 loc) · 1.02 KB
/
4a.c
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
51
52
53
54
55
56
//Ques4. Write a program showing the implementation of pointers to functions.
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
void swap(char* str, int p1, int p2);
int main(int argc, char const *argv[])
{
char* str;
printf("Enter string:\n");
str = (char*)malloc(sizeof(char));
int j;
for (j= 0; *(str + j-1) != '\n'; j++){
str = (char*)realloc(str, (j+2)*sizeof(char));
char c = getchar();
*(str + j) = c;
}
*(str + j - 1) = '\0';
int size = strlen(str);
printf("Position Character Address Of Pointer\n\n");
printf(" Original string:\n");
for (int i = 0; i < size; ++i)
{
printf("%2d\t\t\b%c\t%p\n",i+1, *(str+i), (str+i) );
}
for (int k = 0; k < size/2; ++k)
{
swap(str, k, size - 1 -k);
}
printf(" After reversing:\n");
for (int i = 0; i < size; ++i)
{
printf("%2d\t\t\b%c\t%p\n",i+1, *(str+i), (str+i) );
}
return 0;
}
void swap(char* str, int p1, int p2){
char temp = *(str + p1);
*(str + p1) = *(str + p2);
*(str + p2) = temp;
}