answersLogoWhite

0


Best Answer

#include<stdio.h>

int Strlen (const char* str) {

if (!str) return -1; /* invalid argument */

int count;

count=0;

while (*str++) ++count;

return count;

}

int main (void) {

char str[1024];

scanf ("Enter a string: %s\n", str);

printf ("Length of string: %d\n", Strlen(str));

return 0;

}

User Avatar

Wiki User

8y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is a C program to calculate the number of characters in a string?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Are spaces taken as single character in a string?

public class count { public static void main(String[] args) { String string = "1 2 3 4"; char[] array = string.toCharArray(); System.out.println("Number of characters in string: " + string.length()); System.out.println("Number of characters in array: " + array.length); } } Output: Number of characters in string: 7 Number of characters in array: 7 So yes, spaces are taken as single characters in a string.


A C program to print the number of words from given String?

/*We can calculate this with a lot of methods I'll explain only one of them */ *str = "abcd"; // this is a input string printf("%d", strlen(str));


Count characters in an input string using c sharp?

Console.WriteLine("Please input a string:"); string str = Console.ReadLine(); Console.WriteLine("Number of characters: " + str.Length);


How to write A Java program to print number of digits in a given number?

One way to do this is to convert the number to a String, then use the corresponding String method to find out the length of the String.


How to write a program in python to print the reverse of a number?

In python, type the following into a document. NOTE: Sentences following a # symbol are comments, and are not necessary for the program to run. #!/usr/bin/python #This program takes a input from a user and reverses it. number = input("Type a number: ") #This takes input from a user. a = len(number) #This finds the length of the number reverse = "" #This sets the variable reverse to an empty string. for i in number: a = a-1 #The places in a string start from 0. The last value will be the length minus 1.reverse = reverse + number[a] #Makes the number's last digit the new string's first. print("The reverse of", number, "is", reverse + ".") #prints the resulting string. This program will take any sequence of characters and reverse them. The final value is a string, but if an integer is needed, one needs only to add the line reverse = int(reverse) above the print statement. However, this will stop the program from being able to reverse strings, as it is not possible to convert a string to an integer if it is not a number.


How do you find the no of characters and numerics in the given string?

First of all numbers are not meant to be stored in a string and if u do so in C, these numbers will be treated as characters and you can not perform any calculation on these numbers, and to find the number of characters in a string use the strlen() from the string.h file.


What is the constant string length method?

To get the length of the string we use length property. The length property returns the length of a string (number of characters we use).If the string is empty, length of the string will be 0. Syntax: string.length Hope this helps.


How do you count the number of words in a string?

To find the length of the string we use length method. The length property returns the length of a string (number of characters we use).The length of an empty string is 0. For example: function myFunction() { var str = &quot;Hello World!&quot;; var n = str.length; Hope this hepls.


How do you write c program to accept a string from the console and count number of vowels constants digits tabs and blank spaces in a string?

Read the characters one at a time, and write an "if" for each of the cases. In each case, if the condition is fulfilled, increment the corresponding counter variable.


What program can be written to count the number of alphanumerics in a string?

public int getStringLength(String val) { return val.length(); } There is an inbuilt functionality in strings that counts the number of alphabets in a string called length()


How do you input the number of characters in java?

".length()". The . length method is inherited from the String class.


What elements are in string?

A string is an array of characters.