Question

Write a function that can return the length of a string in C language. Please leave...

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();

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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:

lengthofstr$ gcc los.c lengthofstrs ./a.out Input String: Helloworld The length is 10

----------------------------------------------

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.

Add a comment
Know the answer?
Add Answer to:
Write a function that can return the length of a string in C language. Please leave...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 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 O...

    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...

    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...

    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...

  • C Language! Please, comment what each line of code does in the code below and answer...

    C Language! Please, comment what each line of code does in the code below and answer this question: (sentence is fine) 1. Aside from pressing ctrl-(d/z) at the beginning of a line causing the getnchar() function to return NULL, is there any way to enter less than 9 characters? Code: void exer1(void) {    char input[LEN];    char *check; getchar(); // Clearing character buffer. printf("Please enter 9 characters: "); // Prompting the user to enter // a specific input.   ...

  • Syntax checker. We will write C code to check to see if an input string can be derived from a gra...

    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...

    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...

    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...

    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) {...

    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) {...

    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; }

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT