Answer to Question #321181 in C++ for birbo

Question #321181

Write a program to find length of string by using pointers


1
Expert's answer
2022-04-10T17:45:41-0400
#include <iostream>


int length(char* string) {
  int i = 0;
  // Increment i while character at i'th position in the string
  // is not null-terminator that indicates string end
  for (;string[i] != '\0'; ++i);
  return i;
}


int main() {
  // Maximum length of string
  const int MAX_LENGTH = 10000;
  char* string;
  // Read symbols until newline
  std::cout << "Please enter your string: ";
  std::cin.getline(string, MAX_LENGTH);
  int l = length(string);
  std::cout << "String length is " << l << '\n';
  return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog