Daný reťazec str, úlohou je nájsť dĺžku reťazca.
Príklady:
Vstup: str = Geeks
Výkon: Dĺžka struny je: 5Vstup: str = techcodeview.com
Výkon: Dĺžka struny je: 13
Prístup 1: Iteratívny (pomocou slučky)
Najtradičnejšou metódou zisťovania dĺžky reťazca je prechádzanie každého znaku cez slučku.
- Pomocou počítadla prejdite každý znak reťazca pomocou slučky.
- Aktualizujte počítadlo pre každú postavu
- Keď je reťazec ukončený alebo je identifikovaný nulový znak, prerušte cyklus.
- Vráti hodnotu počítadla ako dĺžku reťazca.
Nižšie je uvedená implementácia vyššie uvedenej metódy:
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;> }> |
>
čo je základná zostava ubuntu
>
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
java print
// 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);> |
>
java dátum na reťazec
>Výkon
13>
Časová zložitosť: O(N), kde N je dĺžka reťazca.
Pomocný priestor: O(1)
Prístup 2: Použitie vstavaných metód
Každý programovací jazyk ponúka aj vstavanú metódu na nájdenie dĺžky reťazca, ako napríklad:
Programovací jazyk | In-Built metóda na zistenie dĺžky reťazca |
---|---|
C | strlen() |
C++ | veľkosť () |
Java | dĺžka () |
Python | len() |
Javascript | dĺžka |
C# | dĺžka () |
Nižšie je uvedená implementácia vyššie uvedených metód:
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
preťaženie metódy
/*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);> > }> }> |
>
>
Python
# 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
začína javou
// 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);> |
>
>Výkon
13>
Časová zložitosť: O(1) , To je xcept strlen() pre C je O(N)
Pomocný priestor: O(1)