int count(char *s, char c);
.section .rodata
prompt:
.string "Enter a string: "
prompt2:
.string "Enter a character: "
.section .text
.globl _start
.type _start, @function
_start:
nop
nop
nop
nop
nop
nop
nop
nop
call read_string
call read_char
call count
call print_int
call exit
.type count, @function
count:
pushl %ebp
movl %esp, %ebp
movl 8(%ebp), %eax
movl 12(%ebp), %edx
movl $0, %ecx
jmp loop
loop:
movl (%eax), %ebx
cmpl %edx, %ebx
jne skip
addl $1, %ecx
skip:
addl $1, %eax
make printbin
./printbin
translateWed, 08 Mar 2023 #include <stdio.h>
int main() {
int x = 42;
printf("%d\n", x);
return 0;
}
.code32
.global count
count:
pushl %ebp
movl %esp, %ebp
movl 8(%ebp), %eax
movl 12(%ebp), %edx
movl $0, %ecx
movl $0, %esi
loop:
movl (%eax), %ebx
cmpl %edx, %ebx
je increment
addl $4, %eax
cmpl $0, (%eax)
je done
jmp loop
increment:
incl %ecx
addl $4, %eax
cmpl $0, (%eax)
je done
jmp loop
done:
popl %ebp
ret
python
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
for i in range(len(nums)):
for j in range(i+1,len(nums)):
if nums[i]+nums[j]==target:
return [i,j]
int count (char *string, char c){
int count = 0;
while (*string != 0){
if (*string == c){
count += 1;
}
string += 1;
}
return count;
}
translateSat, 04 Mar 2023 ++
vector<int> twoSum(vector<int>& nums, int target) {
unordered_map<int, int> map;
for (int i = 0; i < nums.size(); i++) {
int complement = target - nums[i];
if (map.count(complement) and map.find(complement)->second != i) {
return vector<int> {i, map.find(complement)->second};
}
map[nums[i]] = i;
}
return vector<int> ();
}
The function takes two arguments: a pointer to string and the character to be counted. These arguments are passed through the stack. The function allocates the stack frame and then loops through the string and increments the count each time the character is found.
translateSat, 04 Mar 2023 ++
vector<int> twoSum(vector<int>& nums, int target) {
unordered_map<int, int> m;
for (int i = 0; i < nums.size(); ++i) {
m[nums[i]] = i;
}
for (int i = 0; i < nums.size(); ++i) {
int t = target - nums[i];
if (m.count(t) && m[t] != i) {
return {i, m[t]};
}
}
return {};
}
* **Explanation** - explain what your code does.
* **Code** - show your code. Do not just copy and paste it. Embed the code in your markdown file.
* **Example** - show the output of your code.
### Part 2:
This part will be presented in the form of a paragraph.
> For this part you will need to write a paragraph that explains the concept. You will need to use the terms from the rubric.
> The paragraph should be between 5 and 8 sentences.
### Part 3:
This part will be presented in the form of a paragraph.
> For this part you will need to write a paragraph that explains the concept. You will need to use the terms from the rubric.
> The paragraph should be between 5 and 8 sentences.
### Part 4:
This part will be presented in the form of a paragraph.
> For this part you will need to write a paragraph that explains the concept. You will need to use the terms from the rubric.
> The paragraph should
#include<stdio.h>
#include<string.h>
char *printbin(char);
int main(void)
{
char ch, *str;
printf("Enter character: ");
scanf("%c", &ch);
str = printbin(ch);
printf("The binary format for character %c is %s\n", ch, str);
return 0;
}
#### 1.2:
linux bash command that: change directory to "test"
## Source Code:
int count(char *str, char c)
{
int count = 0;
while (*str != '\0')
if (*str++ == c)
count++;
return count;
}
translateSat, 04 Mar 2023 int* twoSum(int* nums, int numsSize, int target, int* returnSize){
int *rest = (int *)malloc(sizeof(int) * 2);
rest[0] = -1;
rest[1] = -1;
for (int i = 0; i < numsSize; i++)
{
for (int j = i + 1; j < numsSize; j++)
{
if (nums[i] + nums[j] == target)
{
rest[0] = i;
rest[1] = j;
break;
}
}
if (rest[0] != -1)
break;
}
*returnSize = 2;
return rest;
}
int count (char *string, char c) {
int count = 0;
while (*string != '\0') {
if (*string == c)
count++;
string++;
}
return count;
}
translateWed, 08 Mar 2023 #include <stdio.h>
int main() {
int a = 5;
int b = 7;
int c = a + b;
printf("%d", c);
}
int count (char *string, char c);
int count (char *string, char c)
#include <stdio.h>
int count(char *string, char c);
int main(void) {
char string[100];
char c;
printf("Enter a string: ");
scanf("%s", string);
printf("Enter a character: ");
scanf(" %c", &c);
printf("The character '%c' occurs %d times in \"%s\"\n", c, count(string, c), string);
}
translateWed, 08 Mar 2023 void add(a, b)
{
return a + b;
}
public class Mystrncpy {
public static void main(String[] args) {
char[] s = {'a', 'b', 'c', 'd'};
char[] ct = {'e', 'f', 'g', 'h'};
int n = 3;
char[] result = strncpy(s, ct, n);
System.out.println("result = " + Arrays.toString(result));
}
public native char[] strncpy(char[] s, char[] ct, int n);
static {
System.loadLibrary("strncpy");
}
}
__Questions to ask yourself__
- Does the input always have the right format?
- Does the input have the right format?
- Does the input have the right range?
- Does the output have the right range?
- Can we assume the input is valid?
- Are there edge cases?
- Are there some cases that we should consider separately?
__Example__
Example 1:
Input: nums = [2,7,11,15], target = 9
Output: [0,1]
Output: Because nums[0] + nums[1] == 9, we return [0, 1].
Example 2:
Input: nums = [3,2,4], target = 6
Output: [1,2]
Example 3:
Input: nums = [3,3], target = 6
Output: [0,1]
__Pseudocode__
translateTue, 07 Mar 2023 #include <stdio.h>
int count(char *str, char c){
int count = 0;
int index = 0;
while(str[index] != '\0'){
if(str[index] == c){
count++;
}
index++;
}
return count;
}
int main(){
char str[100];
printf("Please input a string : ");
scanf("%[^\n]", str);
char c;
printf("Please input a char : ");
scanf(" %c", &c);
int ret = count(str, c);
printf("The char %c appears %d times in the string %s\n", c, ret, str);
return 0;
}
#include <stdio.h>
int count(char *string, char c);
int main(void) {
char c;
char string[100];
int countval;
printf("Please enter a string: ");
scanf("%s", string);
printf("Please enter a character: ");
scanf(" %c", &c);
countval = count(string, c);
printf("Count = %d\n", countval);
return 0;
}
def add(a, b):
return a + b
add(1, 2)
int count(char* str, char c) {
int count = 0;
while (*str != '\0') {
if (*str == c) {
count++;
}
str++;
}
return count;
}
int count(char*, char);
int count(char* string, char c){
int i = 0;
while(*(string + i) != '\0'){
if(*(string + i) == c){
break;
}
i++;
}
return i;
}
make A=count
make run A=count
make script A=count
The function twoSum takes four arguments: array of integers, size of the array, target integer and pointer to size of array which will be returned. The function returns pointer to the array of two elements namely indices of the target number. First we declare pointer to the array of two elements, allocate memory for this array and set pointer to the array of two elements equal to the result. Then we loop through the array and check if the sum of the two elements is equal to the target. If yes, we return the array of two elements that stores the indices of the target number. If not, we return null.
int count(char* str, char c) {
int count = 0;
for (int i = 0; str[i] != '\0'; i++) {
if (str[i] == c) count++;
}
return count;
}
Constraints
1 <= N <= 10^7
0 <= K <= 10^10
0 <= Ai <= 10^9
translateThu, 18 May 2023 Script
//sample input
5 3
10 1 10 1 10
//sample output
3
#include <string.h>
#include <stdio.h>
extern char * printbin(unsigned char c);
int main() {
unsigned char c = 0x3d;
printf("The binary format for character %c is %s", c, printbin(c));
}
translateSat, 04 Mar 2023 unsigned char checksum(unsigned char *data, size_t length) {
unsigned char checksum = 0;
for(int i = 0; i < length; i++) {
checksum ^= data[i];
}
return checksum;
}
translateWed, 08 Mar 2023 #include<stdio.h>
#include<stdlib.h>
int main(){
int a = 10;
printf("%d\n", a);
return 0;
}
### Solution:
translateSat, 04 Mar 2023 int* twoSum(int* nums, int numsSize, int target, int* returnSize){
int i, j;
int *result = (int*)malloc(2*sizeof(int));
for(i=0; i<numsSize; i++){
for(j=i+1; j<numsSize; j++){
if(nums[i] + nums[j] == target){
result[0] = i;
result[1] = j;
*returnSize = 2;
return result;
}
}
}
return NULL;
}
.data
str: .string "hello"
char: .byte 'o'
len: .long 5
.text
.globl count
count: pushl %ebp
movl %esp, %ebp
movl 8(%ebp), %eax
movl 12(%ebp), %edx
movl (%eax), %ecx
movl 4(%ebp), %ebx
loop:
cmpl $0, %ecx
je done
movb (%edx), %al
cmpl %al, (%ebx)
je increment
addl $1, %edx
subl $1, %ecx
jmp loop
increment:
addl $1, %edx
addl $1, %ecx
incl %eax
jmp loop
done:
movl %ebp, %esp
popl %ebp
ret
translateWed, 08 Mar 2023 #include <stdio.h>
#include <stdlib.h>
int add(int x, int y)
{
return x + y;
}
int main()
{
printf("%d\n", add(1, 2));
}
translateSat, 04 Mar 2023 ++
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
unordered_map<int, int> m;
int i = 0;
for (auto n : nums) {
if (m.find(target - n) != m.end()) {
return {i, m[target - n]};
}
m[n] = i++;
}
return {};
}
};
count:
pushl %ebp
movl %esp, %ebp
movl 8(%ebp), %eax
decl %eax
movl %eax, %ebx
movl 12(%ebp), %eax
decl %eax
movl %eax, %ecx
movl $0, %edx
.L3:
cmpl $0, %edx
je .L2
movl (%ebx), %eax
pushl %eax
call getchar
addl $4, %esp
movl %eax, %edx
movl (%ecx), %eax
pushl %eax
call getchar
addl $4, %esp
cmpl %edx, %eax
jne .L4
inc %edx
movl $1, %eax
.L4:
addl $1, %ebx
## Optional Step 4.
You may choose to add additional lines of code. You may also choose to add additional comments.
## Step 5.
Create a new commit in Git and push it to GitHub.
## Step 6.
Create a new branch in GitHub called 'lab6.3-change-file-name'.
## Step 7.
In the new branch, change the file name and push it to GitHub.
translateFri, 17 Mar 2023 #include <stdio.h>
extern void printbin(char c, char *str);
int main(void) {
char c, str[9];
scanf("%x", &c);
printbin(c, str);
printf("The binary format for character %c is %s\n", c, str);
return 0;
}
gcc -o countc countc.c count.s -m32