Question

In: Computer Science

Translate this algorithm to code on C compare(char array[ ]) word = split(array, " "); //gets...

Translate this algorithm to code on C

compare(char array[ ])

word = split(array, " "); //gets the first word before the blank space

if word == Hello {

print("I am saying hello");

}else if (word == Bye)

print("I am saying goodbye");

}

Example---------------------

char array = "Hello Ana";

compare(array);

char array1 = "Bye Ana"

compare(array1);

result

"I am saying hello"

"I am saying goodbye"

Solutions

Expert Solution

#include<stdio.h>

void compare(char arra[ ])
{
if(strlen(arra) >= 3)
{
if(strlen(arra) >= 5 && arra[0] == 'H' && arra[1] == 'e' && arra[2] == 'l' && arra[3] == 'l' && arra[4] == 'o')
printf("I am saying hello");
if(arra[0] == 'B' && arra[1] == 'y' && arra[2] == 'e')
printf("I am saying goodbye");
}
}

int main() {
compare("Hello Ana");
printf("\n");
compare("Bye Ana");
}

// Hit the thumbs up if you are fine with the answer. Happy Learning!


Related Solutions

For c++ Write the code to initialize an array such that each element gets two times...
For c++ Write the code to initialize an array such that each element gets two times the value of its index (e.g. 0, 2, 4, 6, …)
I am running this code using C to count the words in char array, but somehow...
I am running this code using C to count the words in char array, but somehow it keeps counting the total characters + 1. Any solution to fix this problem? #include <stdio.h> int main() {    char input[1001];    int count =0;    fgets(input, 1001, stdin);    char *array = input;    while(*array != '\0')       {        if(*array == ' '){            array++;        }        else{        count++;        array++;...
for C program 10 by 10 char array. char 0-9 as rows and char a-j as...
for C program 10 by 10 char array. char 0-9 as rows and char a-j as collumns.
Translate the following C++ program to Pep/9 assembly language. const char chConst = '+'; char ch1;...
Translate the following C++ program to Pep/9 assembly language. const char chConst = '+'; char ch1; char ch2; int main() { cin.get(ch1); cin.get(ch2);    cout << ch1 << chConst << ch2;    return 0; }
Translate the following C program to Pep/9 assembly language: #include <stdio.h> char toLower(char ch) { if...
Translate the following C program to Pep/9 assembly language: #include <stdio.h> char toLower(char ch) { if (ch >= 'A' && ch <= 'Z' )        ch += 'a' - 'A'; return ch; } int main() { char ch, conversion; printf("Enter an uppercase letter: "); scanf("%c", &ch); conversion = toLower(ch); printf("\nLetter after conversion: %c\n\n", conversion); return 0; }
C Implement the function Append (char** s1, char** s2) that appends the second character array to...
C Implement the function Append (char** s1, char** s2) that appends the second character array to the first, replaces the first array with the result and replaces the second character array with the original first array. For example, if the first character array is "hello" and the second is "world" at the end of the function the new value of the first character array should be"helloworld" and the second array should be "hello". If the input is invalid the function...
Translate the following C++ code to Pseudocode: int main() { stack<char> stk,stk2; int length =0,ele; while(cin>>ele)...
Translate the following C++ code to Pseudocode: int main() { stack<char> stk,stk2; int length =0,ele; while(cin>>ele) { stk.push(ele); length++; } if(length%2) stk.pop(); for (int i=0;i<length/2;i++) { ele=stk.top(); stk.pop(); stk2.push(ele); } int flag=1; for(int i=0;i<length/2;i++) { if(stk.top()==stk2.top()) { stk.pop();stk2.pop(); } else { flag=1; break; } } if(flag==1) cout<<"NOT palindrome"; else cout<<"palindrome"; }
Apply Algorithm split on the array 27 ،13، 31،18، 45 ،16، 17، 53 .
Apply Algorithm split on the array 27 ،13، 31،18، 45 ،16، 17، 53 .
can someone translate this pseudo code to actual c++ code while (not the end of the...
can someone translate this pseudo code to actual c++ code while (not the end of the input) If the next input is a number read it and push it on the stack else If the next input is an operator, read it pop 2 operands off of the stack apply the operator push the result onto the stack When you reach the end of the input: if there is one number on the stack, print it else error
How can i bubble sort a sentence in a char array in c++ This is the...
How can i bubble sort a sentence in a char array in c++ This is the prototype of the function: char* sort(char string[], int numOfWords, int lengthOfWord); This is the testing code in the main file: char words[] = "CAT FAT BAT HAT RAT"; printf("Before sort: t%s\n";words); char result = sort(words; 5; 3); printf("After sort : t%s\n"; result); Expected output: Before sort: CAT FAT BAT HAT RAT After sort: BAT CAT FAT HAT RAT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT