That is O(n).

You swap elements n/2 times: (0 with n-1), (1 with n-2), ... (n/2 - 1 with n/2 + 1)

Answer from Jean Logeart on Stack Overflow
🌐
Unstop
unstop.com › home › blog › reverse a string in c in 10 different ways (+code examples)
Reverse A String In C In 10 Different Ways (+Code Examples)
September 13, 2024 - After the loop terminates, the inputString variable contains the reversed string, which we display using the printf() statement. Complexity: For a string of length n, the time complexity of this method is O(n).
Discussions

What is the time complexity of reversing a string?
It should be simply O(n) where n is the length of string. If you think of a string as an array, you simply swap characters between left and right ends until they cross over the mid-point — which means O(n/2) and simplified to O(n). More on reddit.com
🌐 r/algorithms
13
10
October 23, 2020
Time complexity of bit reverse function in C - Stack Overflow
What is the time complexity of this bit reversing function, if we change the input size to uint_8/uint_16/uint64_t, the for loop runs for the size of the input * 8 times. This functions runs in a constant time for n inputs. More on stackoverflow.com
🌐 stackoverflow.com
algorithm - How to reverse a string in O(1) complexity (runtime)? - Stack Overflow
You cannot reverse a string in O(1) time, however, you can do so with O(1) space complexity. More on stackoverflow.com
🌐 stackoverflow.com
The time complexity to reverse a singly linked list is: A. O(n²) B. O(1) C. O(n) D. O(n log n)
The time complexity to reverse a singly linked list is: A. O(n ... A. O(n²) B. O(1) C. More on askfilo.com
🌐 askfilo.com
1
November 8, 2025
Top answer
1 of 2
1

This algorithm runs in linear time in the size of the input .

To see why try to count how many times you read/write to a position in the string. Note that asymptotically this is equal to the total running time.

2 of 2
0

Have you run the code?

If you reverse the array ['d', 'r', 'a', 'h', ' ', 's', 'i', ' ', 's', 'c'] you get the desired result of ['c', 's', ' ', 'i', 's', ' ', 'h', 'a', 'r', 'd']. If you then reverse the words you get ['s', 'c', ' ', 's', 'i', ' ', 'd', 'r', 'a', 'h'], which is not what you want.

You can't get better than an complexity for reversing an array of size . If you want an explanation as to why this is the case, then we must be slightly more precise. Below I do the analysis for an easy to analyze implementation, not an inplace version. (The analysis is similar for an inplace version, and I think that you can work it out.)

Let's assume that the operations which have a cost are "reading a value from an array", "writing a value to an array", and "comparing two values". The input to our algorithm is an array input of size and the output is an array output also of size , and we must ensure that when the algorithm terminates that output[i] == input[n-i-1] where . (Take a minute and convince yourself that this is a correct description of the problem, and that the equality is correct for both even and odd values of .) To make the analysis as simple as possible, we will assume that there is an array named output of size just waiting for us, and that output[i] == 0 for all .

Therefore, any algorithm must assign to output[i] the value input[n-i-1] for all possible values of , unless input[n-i-1] == 0, in which case no assignment needs to be made, as the correct value is already present. So one possible implementation would loop through the different values of and check if input[n-i-1] == 0 and continue when the condition was true and assign the value to output[i] when the condition was false.

If the input array is 0s then this implementation would perform exactly operations, one operation to get the value at input[n-i-1] and one operation to compare it to 0. However, if there are no values of 0 in the input, then there are exactly operations performed, of the operations are the same as before, but now there are assignments of the value to the correct position in the output array (here we assume we don't need to read the value from the input array again). More generally if 0 occurs times in the input then there are exactly operations performed.

Now this part is important: if we don't check the value, but instead just do the assignment then there are always operations, one to read from the input and one to write to the output. Checking the value each time to avoid an unnecessary copy does more work than not checking the value in the majority of cases. Since any algorithm which uses these operations must do at least this much work, you can't do better than operations.

But what about other operations? For an inplace version you might consider an operation which swaps two values in the array swap(i, j). Such an operation allows you to reverse the array in operations (for even ), which is still .

🌐
Reddit
reddit.com › r/algorithms › what is the time complexity of reversing a string?
r/algorithms on Reddit: What is the time complexity of reversing a string?
October 23, 2020 -

I'm getting ready for an interview and practicing time complexity and I'm ok at recognizing the complexity for the code I've written myself but have a harder time when I throw in methods from the java library.

For example, I read that the method concat was similar to += and that concat time complexity was O(n^2). So if I reverse a string with the code below does that mean my time complexity is O(n^2)? With a space complexity of O(n) since? Where n is the length of the string.

String str="hello"
String s="";
for(int i =str.length()-1; i>=0;i--){
s+=str.substring(i,i+1);
}

With this code is the space complexity O(n) and the time complexity O(n)? Where n is the length of the string.

String str="hello";
char[] cArr = str.toCharArray();
int i =0;
int j = cArr.length-1;
char temp;
while(i<j){
    temp =cArr[i];
    cArr[i]=cArr[j];
    cArr[j]=temp;
    i++;
    j--;
}
str=String.valueOf(cArr);
Top answer
1 of 5
17
It should be simply O(n) where n is the length of string. If you think of a string as an array, you simply swap characters between left and right ends until they cross over the mid-point — which means O(n/2) and simplified to O(n).
2 of 5
8
I don't know the time complexity of internal java lib functions by heart- I'll leave it to someone else to comment on that. What I do want to offer is a technique to sanity check your understanding. Asymptotic time complexity is very much on the numerical / theoretical side of things, but the actual computation time will reflect this behavior once the input size grows large enough. What you can do is repeatedly time the function on various input sizes spanning many orders of magnitude until you reach some limit (probably where the computation starts taking multiple seconds). You can then graph input size vs computation time, and see if its constant, linear, quadratic, logarithmic, etc., by eye. If you want a more precise measurement, take the log of both the input sizes and the computation times (so its a log-log plot), and a best-fit line through your data points (using a linear regression), the slope is the exponent of your variable in your measured time complexity (so a slope of 2 corresponds to observed quadratic time behavior). This isn't perfect, there can easily be huge constants throwing off your measured time complexity, you'll likely drop any logarithmic factors, etc. Its more of a coarse sanity check- if you think the algorithm is O(N2 ) but you experiment and see a near-straight line, and the log-log plot has a slope of 1, then you can reason that its likely not quadratic, its behaving more as a linear-time algorithm, and you should recheck your calculations.
🌐
Stack Overflow
stackoverflow.com › questions › 52746514 › time-complexity-of-bit-reverse-function-in-c
Time complexity of bit reverse function in C - Stack Overflow
I am concerned about the time complexity of the bit reverse function for this problem. ... O(n), for n bits. For uint_8, the algorithm runs in 8 steps. For uint_16, the alrogithm runs in 16 steps.
🌐
Medium
gauravssah.medium.com › reverse-an-array-in-c-efficient-array-reversal-algorithm-56f014d072ae
Problem: Reverse an Array. Learn how to reverse an array in C++… | by Gaurav Sah | Medium
September 20, 2024 - Final Output: After the loop completes, the reversed array revarr is returned. Time Complexity: The time complexity is O(n) where n is the number of elements in the array, because each element is accessed and copied exactly once.
Find elsewhere
🌐
Interviewing.io
interviewing.io › questions › reverse-string
How to Reverse a String [Interview Question + Solution]
September 13, 2018 - The time complexity is O(n²) because each time we append a character to the end of the string, we end up creating a new string. This new string then gets assigned to the variable reversed_string.
🌐
Quora
quora.com › How-can-you-reverse-a-string-in-a-way-to-get-a-time-and-space-complexity-that-is-better-than-O-n
How to reverse a string in a way to get a time and space complexity that is better than O(n) - Quora
A string is a sequence, and reversing the sequence always entails the entire sequence, thus \mathcal{O}(n). If the string is 100 characters long, it will take processing 100 chars to reverse it.
🌐
GeeksforGeeks
geeksforgeeks.org › dsa › reverse-a-string
Reverse a String - GeeksforGeeks
The idea is to use recursion and define a recursive function that takes a string as input and reverses it. Inside the recursive function, Swap the first and last element. Recursively call the function with the remaining substring.
Published   3 weeks ago
🌐
Wyzant
wyzant.com › resources › ask an expert
Array reversal in O( log n) time. | Wyzant Ask An Expert
January 19, 2024 - Reversing an array in O(log n) time complexity is not possible. The reason for this is that reversing an array involves swapping elements, and to perform a swap operation for each element in an array, you would need to traverse the entire array at least once.
🌐
Scaler
scaler.com › home › topics › c program to reverse an array
C Program to Reverse an Array | Scaler Topics
May 4, 2023 - Therefore, its space complexity is ... There are no inbuilt functions in the C programming language to reverse an array, we will have to implement the methods on our own to do the same.
🌐
Quora
quora.com › Why-does-reversing-an-array-take-linear-time
Why does reversing an array take linear time? - Quora
Answer (1 of 5): Well let’s remember what the definition of Big-Oh is: Let f(n) and g(n) be complexity/growth functions. Then, f(n) \in O(g(n)) if and only if there exist positive real constant c and positive integer constant n_0, such that for all n \geq n_0, f(n) \leq c \cdot g(n). Let n be t...
🌐
Filo
askfilo.com › higher education › smart solutions › the time complexity to reverse a singly linked list is: a. o(n
The time complexity to reverse a singly linked list is: A. O(n²) B. O(1)..
November 8, 2025 - To reverse a singly linked list, we need to traverse the entire list once and change the direction of the next pointers for each node. If there are n nodes in the list, each node is visited exactly once. ... Each node is processed once, and the operation for each node (changing the pointer) takes constant time. Therefore, the total time is proportional to the number of nodes, i.e., O(n).
🌐
Superchargedcomputing
superchargedcomputing.com › 2018 › 04 › 17 › reverse-in-olog-n-time
reverse in O(log n) time – Supercharged Computing
July 11, 2018 - The possible implementation of reversing a string/vector is given as follows which takes O(n) time where n is size of collection string/vector. template void reverse(BidirIt first, BidirIt last) { while ((first !=…
🌐
InterviewBit
interviewbit.com › coding problems › reverse string (c++, java, and python)
Reverse String (C++, Java, and Python) - InterviewBit
June 23, 2023 - public void helper(char[] s, int ... helper(left + 1, right - 1) helper(0, len(s) - 1) Time Complexity:O(N), where N is the length of the string....
🌐
DataFlair
data-flair.training › blogs › c-program-to-reverse-a-number-using-recursion
C Program to Reverse a Number using Recursion - DataFlair
March 9, 2024 - The reverse function is called with num, and the result is stored in the reversed variable. Finally, the reversed number is printed using printf. This recursively extracts and accumulates each digit to build the final reversed number. The time complexity of the algorithm is denoted as O(n), where ‘n’ signifies the count of digits in the input.
🌐
Sololearn
sololearn.com › en › Discuss › 2445429 › what-will-be-the-time-complexity-when-functions-like-reverse-is-used
What will be the time complexity when functions like reverse is used? | Sololearn: Learn to code for FREE!
alagammai uma the time complexity of reverse method would be O(n) only. You cannot reverse it faster than that since you need to iterate over each character atleast once.
🌐
Verve AI
vervecopilot.com › interview-questions › what-no-one-tells-you-about-reverse-the-string-c-and-interview-performance
What No One Tells You About reverse the string c++ and Interview Performance
Pop Characters: Iterate again, popping characters from the stack and placing them back into the string. This method has O(N) time complexity (two passes over the string) and O(N) space complexity due to storing all characters in the stack.