Description
TAs: Seydanur Ahi, Sibel Gulmez and Basak Karakas
Write two C programs that performs the task described below. Name your c files as nameSurnameId questionNo.c [ex: basakKarakas1850044026 1.c and basakKarakas1850044026 2.c]
Upload your file to your section’s submission page on moodle.
1 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> void reverse_string(char str[]);
int main(){ char str_arr[100];
printf(“Enter a string:”); scanf(“%s”, str_arr); reverse_string(str_arr);
printf(“Reversed string is: %s “, str_arr);
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
1
2. Write print line(), print histogram() and len() functions for the given code below. printline() : 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> void print_line(char c, int num_of_chars); void print_histogram(char c, int vals[]); int len(int arr[]);
int main(){ int values[100], val=1, count=0; printf(“Enter positive integer(s), to print an
histogram. Enter a non-positive integer to stop. “); do{ printf(“Enter a value:”); scanf(“%d”, &val); values[count] = val; count++; }while(val > 0); print_histogram(’*’, values); return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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




Reviews
There are no reviews yet.