C++ School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!
 

Code Snippets

  

C++ Source Code



String Array Length

This simple little snippet will tell you the length of a string array. It might be useful to someone. But it was a simple exercise for me as I learn C++

Submitted By: enj316
Actions:
Rating:
Views: 18,575

Language: C++

Last Modified: June 16, 2006
Instructions: This pretty simple, where message and message2 are in the main function you can enter a string and the function StringLength will tell you how many characters are in the string.
You can create many varitions with this snippet.

Snippet


  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int StringLength(char inputString[])
  6. {
  7.     int length = 0;
  8.    
  9.     for (int i = 0; inputString[i]!= '\0'; i++)
  10.        
  11.         length++;
  12.    
  13.     return length;
  14. }
  15.  
  16. int main()
  17. {
  18.     char message[] = "C++ Program";
  19.     char message2[] = "Wow this is great.";
  20.     cout << "The length of message is " << StringLength(message) <<endl <<endl;
  21.     cout << "The length of message2 is " << StringLength(message2) <<endl <<endl;
  22.     system("pause");
  23.     return(0);
  24. }
  25.  

Copy & Paste


Comments

There are currently no comments for this snippet. Be the first to comment!

Add comment


You must be registered and logged on to </dream.in.code> to leave comments.