Encuentra la longitud de una cuerda

dada una cuerda cadena, la tarea es encontrar la longitud de la cuerda.

Ejemplos:

Aporte: str = frikis
Producción: La longitud de Str es: 5

Aporte: str = techcodeview.com
Producción: La longitud de Str es: 13

Método 1: iterativo (usando bucle)

El método más tradicional para encontrar la longitud de la cadena es atravesar cada carácter a través del bucle.

  • Usando un contador, recorre cada carácter de la cadena con la ayuda de Loop.
  • Actualiza el contador de cada personaje.
  • Cuando la cadena termine o se identifique un carácter nulo, rompa el bucle.
  • Devuelve el valor del contador como la longitud de la cadena.

A continuación se muestra la implementación del método anterior:

C++




// C++ program to find length> // of a string> #include> using> namespace> std;> // Driver code> int> main()> {> > // String obj> > string str => 'techcodeview.com'> ;> > // The constructor of string will set> > // it to the C-style string,> > // which ends at the ' '> > // size of string object Using while loop> > // while 'NOT NULL'> > int> i = 0, cnt = 0;> > while> (str[i]) {> > i++;> > cnt++;> > }> > cout < < cnt < < endl;> > return> 0;> }>

C




// C program to find the length of string> #include> #include> int> main()> {> > char> Str[] => 'techcodeview.com'> ;> > int> i = 0, cnt = 0;> > while> (Str[i]) {> > cnt++;> > i++;> > }> > printf> (> '%d'> , cnt);> > return> 0;> }>

Java




public> class> StringLength {> > public> static> void> main(String[] args) {> > // String object> > String str => 'techcodeview.com'> ;> > // Initialize a variable to count the characters> > int> i => 0> , cnt => 0> ;> > // Use a while loop to iterate through the characters until the end of the string> > while> (i i++; cnt++; } // Print the length of the string System.out.println(cnt); } }>

Python3




# Python program to find length> # of a string> # String obj> str> => 'techcodeview.com'> # size of string object Using while loop> i> => 0> cnt> => 0> while> str> [i:]:> > i> +> => 1> > cnt> +> => 1> print> (cnt)>

C#




using> System;> class> Program {> > static> void> Main(> string> [] args) {> > // String object> > string> str => 'techcodeview.com'> ;> > // Use the Length property to get the length of the string> > int> length = str.Length;> > // Print the length of the string> > Console.WriteLine(length);> > }> }>

JavaScript




// String object> let str => 'techcodeview.com'> ;> // Initialize a variable to count the characters> let i = 0, cnt = 0;> // Use a while loop to iterate through the characters until the end of the string> while> (str[i] !== undefined) {> > i++;> > cnt++;> }> // Print the length of the string> console.log(cnt);>

Producción

13 

Complejidad del tiempo: O(N), donde N es la longitud de la cuerda.
Espacio auxiliar: O(1)

Enfoque 2: uso de métodos integrados

Cada lenguaje de programación también ofrece un método integrado para encontrar la longitud de la cadena, como por ejemplo:

Lenguaje de programación

Método incorporado para encontrar la longitud de la cuerda

C

strlen()

C++

tamaño()

Java

longitud()

Pitón

solo()

JavaScript

longitud

C#

longitud()

A continuación se muestra la implementación de los métodos anteriores:

C++




// C++ program to find length> // of a string> #include> #include> using> namespace> std;> // Driver code> int> main()> {> > // String obj> > string str => 'techcodeview.com'> ;> > // size of string object using size() method> > cout < < str.size() < < endl;> > return> 0;> }>

C




// C program to find the length of> // string using strlen function> #include> #include> int> main()> {> > char> Str[] => 'techcodeview.com'> ;> > printf> (> '%ld'> ,> strlen> (Str));> > return> 0;> }>

Java




/*package whatever //do not write package name here */> import> java.io.*;> class> GFG {> > public> static> void> main(String[] args)> > {> > String str => 'techcodeview.com'> ;> > int> stringSize = str.length();> > System.out.println(stringSize);> > }> }>

Pitón




# Python code to demonstrate string length> # using len> str> => 'techcodeview.com'> print> (> len> (> str> ))>

C#




using> System;> class> Program> {> > static> void> Main()> > {> > // String variable> > string> str => 'techcodeview.com'> ;> > // Length of the string using the Length property> > Console.WriteLine(str.Length);> > // Alternatively, you can also use the String.Length method:> > // Console.WriteLine(str.Length);> > // Pause the program execution to see the result> > Console.ReadLine();> > }> }> //Contributed by Aditi Tyagi>

JavaScript




// String object> let str => 'techcodeview.com'> ;> // Use the `length` property of the string object to get its length> // The `length` property directly gives the length of the string> let length = str.length;> // Print the length of the string> console.log(length);>

Producción

13 

Complejidad del tiempo: O(1) , Es xcept strlen() para C es O(N)
Espacio auxiliar: O(1)