This is C programming. Please fill in the blank. Code is to copy string and print backward. /* Requirement: We’ll initialize a string (I’ll use “Hello World”). Then, create a second string and copy the first string over, but backwards, using a loop. Print both strings. For example: string 1 is “Hello World”, string 2 would be “dlroW olleH”. */ /*Copy Strings Backward Input String1: Hello World*/ #include #define str_len __1__ //str_len = hello + space + world + null char LEN __2__ print_string(__3__ str[__4__]); __5__ copy_string(__6__ s1[__7__], __8__ s2[__9__]); int main() { __10__ str1[]= "Hello World"; __11__ str2[__12__]; //loop and copy each character over // until the array reaches null character copy_string(__13__, __14__); //printing back by calling function print_string printf("String1: "); print_string(__15__); printf("\nString2: "); print_string(__16__); } /* Copy one string to another by looping backward. Need 2 index variables to keep track of each string. For ex: string1 = "hello", len = 6 (1 for null character) string2[0] = string1[4] // letter 'o' string2[1] = string2[3] // letter 'l' Notice the index value compare to the len. */ __17__ copy_string(__18__ s1[__19__], __20__ s2[__21__]) { int idx1, idx2 = 0; //this is the index counter for the 2 strings s1 and s2 /* be careful here. We'll loop from the end of str1 to get letter 'd' normally, for array, last character is size-1, but for string, the last index is the null character, not the last character */ for(idx1= str_len - __22__; idx1 >= __23__ ; idx1 __24__) { /*Copy each char from string s1 to string s2*/ __25__[__26__] = __27__[__28__]; /*idx1 for s1 is updated with for loop update idx2 to save in the next location of s2.*/ idx2++; } //After reading all characters,need to add something in the last //index position to let the program knows the string has ended. s2[__29__] = '__30__'; } /* Printing all letter until the null character is reached. printf("%s", str) would work, but we are doing this way to make sure we understand how string works. Loop through all character until it reaches null character */ __31__ print_string(__32__ str[str_len]) { for(int i = 0; str[i] != __33__; i++) printf("%c", __34__); }
Control structures
Control structures are block of statements that analyze the value of variables and determine the flow of execution based on those values. When a program is running, the CPU executes the code line by line. After sometime, the program reaches the point where it has to make a decision on whether it has to go to another part of the code or repeat execution of certain part of the code. These results affect the flow of the program's code and these are called control structures.
Switch Statement
The switch statement is a key feature that is used by the programmers a lot in the world of programming and coding, as well as in information technology in general. The switch statement is a selection control mechanism that allows the variable value to change the order of the individual statements in the software execution via search.
This is C
/*
Requirement: We’ll initialize a string (I’ll use “Hello World”). Then, create a
second string and copy the first string over, but backwards, using a loop. Print both strings.
For example: string 1 is “Hello World”, string 2 would be “dlroW olleH”.
*/
/*Copy Strings Backward Input String1: Hello World*/
#include<stdio.h>
#define str_len __1__ //str_len = hello + space + world + null char LEN
__2__ print_string(__3__ str[__4__]);
__5__ copy_string(__6__ s1[__7__], __8__ s2[__9__]);
int main()
{
__10__ str1[]= "Hello World";
__11__ str2[__12__];
//loop and copy each character over
// until the array reaches null character
copy_string(__13__, __14__);
//printing back by calling function print_string
printf("String1: ");
print_string(__15__);
printf("\nString2: ");
print_string(__16__);
}
/*
Copy one string to another by looping backward. Need 2 index variables to keep track of each string. For ex: string1 = "hello", len = 6 (1 for null character) string2[0] = string1[4] // letter 'o' string2[1] = string2[3] // letter 'l' Notice the index value compare to the len.
*/
__17__ copy_string(__18__ s1[__19__], __20__ s2[__21__])
{
int idx1, idx2 = 0; //this is the index counter for the 2 strings s1 and s2
/*
be careful here. We'll loop from the end of str1 to get letter 'd' normally, for array, last character is size-1, but for string, the last index is the null character, not the last character
*/
for(idx1= str_len - __22__; idx1 >= __23__ ; idx1 __24__)
{
/*Copy each char from string s1 to string s2*/
__25__[__26__] = __27__[__28__];
/*idx1 for s1 is updated with for loop update idx2 to save in the next location of s2.*/
idx2++;
}
//After reading all characters,need to add something in the last
//index position to let the program knows the string has ended.
s2[__29__] = '__30__';
}
/*
Printing all letter until the null character is reached. printf("%s", str) would work, but we are doing this way to make sure we understand how string works. Loop through all character until it reaches null character
*/
__31__ print_string(__32__ str[str_len])
{
for(int i = 0; str[i] != __33__; i++)
printf("%c", __34__);
}
Step by step
Solved in 3 steps with 1 images