$29
Write two C programs that performs the task described below. Name your c les as nameSurnameId questionNo.c [ex: basakKarakas1850044026 1.c and basakKarakas1850044026 2.c]
Upload your le to your section’s submission page on moodle.
• Task
1. Write the reverse string() function for the given code below. (Hint: use strlen() function which gives the length of a string.)
• # include < stdio .h >
• # include < string .h >
3
4
void
r e v e r s e _ s t r i n g ( char str []) ;
5
6
int
main () {
7
char str_arr [100];
8
9
printf ( " Enter a string :") ;
10
scanf ( "%s" , str_arr ) ;
11
r e v e r s e _ s t r i n g ( str_arr ) ;
12
printf ( " Reversed string is: %s \n" , str_arr ) ;
13
14
return 0;
15 }
16
1
2. Write print line(), print histogram() and len() functions for the given code below.
print line() : Prints <int num of chars> times <char c> character.
print histogram() : Prints a histogram of <int values[]> with <char c> using print print line() and len() functions.
len() : Returns the length of <int array[]>.
• # include < stdio .h >
2
3
void
p r i n t _ l i n e ( char c , int
n u m _ o f _ c h a r s ) ;
4
5
void
p r i n t _ h i s t o g r a m ( char
c ,
int vals []) ;
6
7
int
len ( int arr []) ;
8
9
int
main () {
10
int values [100] , val =1 ,
count =0;
11
printf ( " Enter positive
integer (s), to print an
h is to gr a m .\ nEnter a non - positive
integer to stop .\n\n" ) ;
12
do {
13
printf ( " Enter a value :" ) ;
14
scanf ("%d" , & val ) ;
15
values [ count ] = val ;
16
count ++;
17
} while ( val > 0) ;
18
p r i n t _ h i s t o g r a m ( ’*’ , values ) ;
19
return 0;
20 }
21
Sample:
Enter positive integer(s), to print an histogram.
Enter a non-positive integer to stop.
Enter a value: 3
Enter a value: 5
Enter a value: 6
Enter a value: 2
Enter a value: -1
Output:
***
*****
******
**
Good luck :)
2