fork download
  1. /*******************************************************************************
  2.  * Aiden Stannard // CS1A // Chapter 10 Homework: Challange 1# // 11/28/2026
  3.  * _____________________________________________________________________________
  4.  *
  5.  * Length Finder
  6.  * _____________________________________________________________________________
  7.  *
  8.  * This program returns the length of a c-string from a given c-string.
  9.  * _____________________________________________________________________________
  10.  *
  11.  * Input: result = length(ptr);
  12.  *
  13.  * Output: The length of cString is__
  14.  *
  15.  ******************************************************************************/
  16. #include <iostream>
  17. #include <cctype>
  18. #include <cstring>
  19.  
  20. using namespace std;
  21.  
  22. int length(char *);
  23.  
  24. int main (){
  25. char cString[] = "Hello"; // string definition and initialization
  26. char *ptr = cString; // Defines pointer and initializes it to the CString
  27. int result; // Stores function return value
  28.  
  29. result = length(ptr); // passes the pointer to the function "length"
  30.  
  31. cout << "The length of cString is " << result << endl; // outputs function
  32. // return value to the console
  33.  
  34. return 0;
  35. }
  36.  
  37. int length(char *ptr){
  38. int y;
  39. y = strlen(ptr); // strlen function finds the length of a string
  40. return y;
  41. }
  42.  
Success #stdin #stdout 0s 592KB
stdin
Standard input is empty
stdout
The length of cString is 5