Write C++ program to find Length of the String by passing String/Character


I have used CodeBlocks compiler for debugging purpose. But you can use any C++ programming language compiler as per your availability.


techstudy.org

HP 15 Core i3 7th gen Laptop(4GB, 1TB HDD, Windows 10) | Rs. 31,490

Introduction

I have used CodeBlocks compiler for debugging purpose. But you can use any C++ programming language compiler as per your availability.


  1. #include <iostream>
  2. #include <math.h>
  3. using namespace std;
  4.  
  5. //function declaration
  6. int stringLength(char *);
  7.  
  8. int main()
  9. {
  10. char text[100];
  11. int length;
  12.  
  13. cout<<"Enter text (max- 100 characters): ";
  14. cin>>text;
  15.  
  16. length = stringLength(text);
  17.  
  18. cout<<"Input text is: "<<text<<endl;
  19. cout<<"Length is: "<<length<<endl;
  20.  
  21. return 0;
  22. }
  23.  
  24.  
  25. int stringLength(char *str)
  26. {
  27. int len=0;
  28.  
  29. //calculating string length
  30. for(len=0; str[len]!='\0'; len++);
  31.  
  32. //returning len
  33. return len;
  34. }
  35.  

Result

C++ language techstudy.org

     





Related Topics




Popular Posts