문자열이 주어지면 str, 임무는 문자열의 길이를 찾는 것입니다.
예:
입력: str = 괴짜
산출: Str의 길이는 : 5입력: str = techcodeview.com
산출: Str의 길이는 13입니다.
접근법 1: 반복(루프 사용)
문자열의 길이를 찾는 가장 전통적인 방법은 루프를 통해 각 문자를 탐색하는 것입니다.
- 카운터를 사용하여 루프의 도움으로 문자열의 각 문자를 탐색합니다.
- 모든 캐릭터의 카운터 업데이트
- 문자열이 종료되거나 널 문자가 식별되면 루프를 중단합니다.
- 카운터 값을 문자열 길이로 반환합니다.
위 메소드를 구현하면 다음과 같습니다.
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 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;> }> |
>
>
자바
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); } }> |
왼쪽 조인 vs 오른쪽 조인
>
>
파이썬3
# 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)> |
>
>
씨#
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);> > }> }> |
>
>
자바스크립트
// 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);> |
>
>산출
행 대 열
13>
시간 복잡도: O(N), 여기서 N은 문자열의 길이입니다.
보조 공간: 오(1)
접근 방식 2: 내장 메서드 사용
모든 프로그래밍 언어는 다음과 같이 문자열의 길이를 찾는 기본 제공 방법도 제공합니다.
프로그래밍 언어 | 문자열의 길이를 찾는 내장 방법 |
---|---|
씨 | strlen() |
C++ | 크기() |
자바 | 길이() |
파이썬 | 오직() |
자바스크립트 | 길이 |
씨# | 길이() |
위 메소드의 구현은 다음과 같습니다.
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 program to find the length of> // string using strlen function> #include> #include> int> main()> {> > char> Str[] => 'techcodeview.com'> ;> > printf> (> '%ld'> ,> strlen> (Str));> > return> 0;> }> |
>
>
자바
/*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 code to demonstrate string length> # using len> str> => 'techcodeview.com'> print> (> len> (> str> ))> |
>
>
씨#
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> |
>
>
자바 입력 문자열
자바스크립트
// 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);> |
>
>산출
13>
시간 복잡도: 오(1) , 그것은 C의 경우 strlen()을 제외하면 O(N)입니다.
보조 공간: 오(1)