Wednesday 15 June 2011

calculate string length excluding the null character without using library functions in c++? -


After

I am beginning a c ++ and I am struggling to create a program for the following tasks :

Create a project called "string lamps". The main program should include a function (called 'FindString Langement') which will calculate the length of the string (i.e., the characters in the string Number, excluding futile null character) The program should examine the operation of the function with the following test string: "Short string" "A long string in use for testing purposes" "" "Announce four character arrays and assign these test values ​​to the output of the program Should take the form shown below for each test string: length of "small" = 5 to write the code to calculate the length of the string Do not use any library work to do this. Note that if a quotation mark is to be included in the string, it should come before a backslash character - as explained in the end of the string To stop it: "Quotation in string" will be displayed as four: Quotation four in the string

My code is as follows:

 < Code> # include & lt; Iostream & gt; Using namespace STD; Size_t findStringLength (four *); Integer main () {four n1 [] = "short string", four n2 [] = "a long string is used for testing purposes", four n3 [] = ""; Four n4 [] = ""; Integer stringlength; Stringlength = findStringLength ("short string /"); Court & lt; & Lt; & Lt "duration"; & Lt; N1's & lt; & Lt; "=" & Lt; & Lt; Stringlength & lt; & Lt; Endl; Stringlength = findStringLength ("string used for a long-term testing purposes"); The court's & lt; & Lt; "Key \ nLength" & lt; & Lt; N2 & lt; & Lt; "=" & lt; & Lt; Stringlength & lt; & Lt; Endl; Stringlength = findStringLength ("/"); Cout & lt; & Lt; "Length of \ N" & lt; & lt; n3 & lt; & lt; "=" & lt;  

Edit I now have the code shown above, which gives the right output to some extent. The problem is that I am receiving this

Error: H: \ string long \ main.cpp: 16: Warning: String continuously deprecated conversion for 'four *' [-Wwrite-strings] Stringlength = findStringLength ("short string /"); ^

I would suggest a simple and loop instead:

  int String_length = 0; While (input_str [string_length]! = '\ 0') {string_length ++; } Return string_land;  

It runs again with a string until it hits an empty character.


No comments:

Post a Comment