Write a program to find length of string by using pointers
#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;
}
Comments
Leave a comment