answersLogoWhite

0

As this is probably a homework question, I will give you some pseudo code:

[code]

num_chars = 0

READ ch FROM string

WHILE ch IS NOT END OF STRING

num_chars = num_chars + 1

READ ch FROM string

END WHILE

[/code]

Remember that in C, we use what are called "C-strings". C-strings are a pointer to a continuous group of characters in memory which are terminated by a null character. The null character is '\0', and has an integer value of 0.

The C-string generally points to the first character in the string. To access the value of this character, you must use the dereferencing operator, *. If you want to move to the next character, you simply add 1 to the pointer.

So if you have a C-string:

char *str = "abcd";

then:

*str '\0'

Anything past the null character is undefined. Trying to access this data is considered to be a buffer overflow, and is very dangerous.

Note that c-strings created as pointers should always be treated as immutable, as trying to change them might produce errors. Many compilers will allocate the above string inside the static data area, along with any constants or literals which can not fit inside the immediate field of an instruction.

If you want a mutable string, then declare it as a character array:

char str[] = "abcd";

This method of declaration will explicitly allocate memory on the stack to store the c string in, and as such, the string can be safely manipulated without fear of unintended side effects.

User Avatar

Wiki User

14y ago

What else can I help you with?

Related Questions

How do you write a program using function that counts the number of vowels in a string in java language?

Use text-editor notepad++


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.


What is the function of the replace parameter in Oracle?

The function of the replace parameter in Oracle is to replace a sequence of characters in a string with other characters, usually in sets of characters.


Can you provide an example of how to use the keyword "headstring" in a programming function?

In programming, the keyword "headstring" can be used to extract a specific number of characters from the beginning of a string. For example, in a function that takes a string as input and uses "headstring" to extract the first 5 characters, the code might look like this: python def extractheadstring(inputstring): head inputstring:5 return head In this function, the "headstring" keyword is used to extract the first 5 characters from the input string and return them as a new string.


In gets function which will you use to fix the size of the string and specify the number of characters?

Never use gets, use fgets instead. And strlenafterwards.


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 = "Hello World!"; var n = str.length; Hope this hepls.


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


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


What function returns the number of occurrences of a specified character in the input string?

The charAt function returns the number of occurrences of a specified character in the input string.


What function are used for string length in visual basic?

string length is a function use to check the lenght of a string i.e number of alphabets in a word or sentence.


What are differences between replace and substitute Microsoft Excel functions?

The Replace function replaces part of a text string, based on the number of characters you specify, with a different text string. The Substitute function substitutes newtext for old text in a text string. You use Substitute when you want to replace specific text in a text string and you use Replace when you want to replace any text that occurs in a specific location in a text string.


What is a Str function in VB?

Str converts a number to a string.