-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEmbeddedSystemDesign_HW_week3.4.c
94 lines (79 loc) · 2.41 KB
/
EmbeddedSystemDesign_HW_week3.4.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*
**************************************************************
* NOTE: This code has already been modified after submission *
**************************************************************
Make a function that turns the string backwards. The address of the first element of array and length of a string are passed as a
parameter to the function. Length of the string can be between 2-1000.
For example:
Test | Result
---------------------------------------------
char text[]={"test text"}; | txet tset
int len=strlen(text); |
reverse_order(text,len); |
for(int k=0;k<len;k++) |
{ |
printf("%c",text[k]); |
} |
*/
#include <stdio.h>
#include <math.h>
#include <string.h>
//function prototype(s)
void reverse_order(char *text, int len);
/*---------------------------------------------------------------------------------------------------------------------------------*/
//main function
int main(void)
{
char text[]={"esrever ni ydaerla saw txet l4n1g1r0 ehT"};
int len=strlen(text);
//original text
printf("The original text was: ");
for(int h=0; h<len; h++)
{
printf("%c",text[h]);
}
printf("\n");
//reverse text
printf("The text in reverse is: ");
reverse_order(text, len);
for(int k=0; k<len; k++)
{
printf("%c",text[k]);
}
printf("\n");
return 0;
}//end main
/*---------------------------------------------------------------------------------------------------------------------------------*/
//fuction(s) declaration
void reverse_order(char *text, int len)
{
char new_text[len-1];
for(int i=0; i<len; i++)
{
new_text[i] = text[len-1-i];
}
for(int j=0; j<len; j++)
{
text[j] = new_text[j];
}
}//end reverse_order
/*---------------------------------------------------------------------------------------------------------------------------------*/
/*-----------------------------------------------------TEACHER'S ANSWER------------------------------------------------------------*/
/*---------------------------------------------------------------------------------------------------------------------------------*/
/*
void reverse_order(char *incoming_array, int len)
{
char temp_array[len];
for(int i=0;i<len;i++)
{
temp_array[len-1-i]=*incoming_array;
incoming_array++;
}
incoming_array=incoming_array-len;
for(int k=0;k<len;k++)
{
*incoming_array=temp_array[k];
incoming_array++;
}
}
*/