Write a function that can return the length of a string in C language. Please leave comments to explain the code and verify that it runs properly...
Example:
Input String: China
The length of the string is 5.
#include <stdio.h>
int length(char * p)
{
/write your code here
}
main(){
int len;
char str[20];
printf("Input string:");
scanf("%s", str);
len = length(str);
printf("The length of string is %d. ", len);
getchar();
getchar();
}
The following is the C code:
// this is a program to calculate the length of a string
#include <stdio.h>
int length(char *p)
{
int len = 0; // len is the current length of the
string
while (p[len] != '\0') // every string ends with a
null character
{
len++; // if null character wasn't
seen then incremement length by 1
}
return len;
}
int main() // this is the main method
{
int len;
char str[20];
printf("Input String: "); // take an input
string
scanf ("%s", str);
len = length (str); // find the length of the string
using the function created above
printf("The length is %d.\n", len); // print the
length of the string
getchar();
getchar();
return 0;
}
---------------------------------------------
The following is the screenshot of the output:
----------------------------------------------
In the code segment, provided by you scanf has been used to take the input from the user. So, I've kept it as it is in your code. However note that using scanf will take the part of the string till the first space or newline character.
This means if the user enters Hello World, then only Hello is taken by the code.
Write a function that can return the length of a string in C language. Please leave...
Here is a serial program in C and parallel program in OpenMP that takes in a string as input and counts the number of occurrences of a character you choose. Why is the runtime for the output for the OpenMP parallel program much longer? Serial Program #include <stdio.h> #include <string.h> #include <time.h> int main(){ char str[1000], ch; int i, frequency = 0; clock_t t; struct timespec ts, ts2; printf("Enter a string: "); gets(str); int len = strlen(str); printf("Enter a character...
Please use C language with basic terms. Please fast, thank you so much :) Write a function declared as "int categorize(char *str)" which accepts a string as an input parameter and categorize the string. The function should return the category number as decribed below: Category 1: If the string contains only capital letters (e.g. ABCDEF), the function should return 1. Category 2: If the string contains only small letters (e.g. abcdef), the function should return 2. Category 3: If the...
using c language String Challenge Have the function StringChallenge(str) read str which will contain two strings separated by a space. The first string will consist of the following sets of characters: +, *, $, and {N} which is optional. The plus (+) character represents a single alphabetic character, the ($) character represents a number between 1-9, and the asterisk (*) represents a sequence of the same character of length 3 unless it is followed by {N} which represents how many...
Syntax checker. We will write C code to check to see if an input string can be derived from a grammar. The grammar is: A ::= 0 B B ::= 1 A | 2 B | ; Your program "check.c" should output "yes" if the line of standard input can be derived from this grammar, and "no" otherwise. Here are some examples: $ ./check 0 ; yes $ ./check 0 2 1 ; no $ The input values will always...
Please write MIPS program that runs in QtSpim (ex: MipsFile.s) Write a MIPS program that will read in a base (as an integer) and a value (nonnegative integer but as an ASCII string) in that base and print out the decimal value; you must implement a function (which accepts a base and an address for a string as parameters, and returns the value) and call the function from the main program. The base will be given in decimal and will...
Write a program that prompts the user to input a string. The program then uses the function substr to remove all the vowels from the string. For example, if str=”There”, then after removing all the vowels, str=”Thr”. After removing all the vowels, output the string. Your program must contain a function to remove all the vowels and a function to determine whether a character is a vowel. #include <iostream> #include <string> using namespace std; void removeVowels(string& str); bool isVowel(char ch);...
What does this code do? On the flipside of this paper, write the same code in Java. #include <stdio.h> #include <stdlib.h> int main ) ( char *str: (char *) malloc (15) ; str strcpy (str, "Iron"); printf("String 8u\n", str, str); Address %s, (char ) realloc (str, 25); str strcat (str, "Maiden"); Su\n", str, str): Address %s, printf ("String free (str) return (0); What does this code do? On the flipside of this paper, write the same code in Java. #include...
Convert the below C code to basic MIPS. Please leave comments for explanation #include <stdio.h> int main(void) { printf("Insert two numbers\n"); int a,b; scanf("%d",&a); scanf("%d",&b); a=a<<2; b=b<<2; printf("%d&%d\n",a,b); return 0; }
Convert the below C code to basic MIPS. Leave comments for explanation please #include <stdio.h> int main(void) { printf("Insert two numbers\n"); int a,b,c; scanf("%d",&a); scanf("%d",&b); c=a|b; printf("%d|%d=%d\n",a,b,c); return 0; }
Write a program in C that reads a string of bits( so either a one or zero) in from the user one char at a time using the function getChar, which returns a char. hint in order to convert a char to an int, subtract the character. Then store the bits into an array. start with this: #include "stdio.h" #define MAX_BITS 32 int main() { printf("Enter up to 32 bits (hit 'enter' to terminate early): "); char bit = getchar();...