Code Snippets

  

C++ Source Code


Welcome to Dream.In.Code
Become a C++ Expert!

Join 148,794 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,171 people online right now. Registration is fast and FREE... Join Now!





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: 13,601

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.




Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month