0

The code is supposed to reverse the words that have the length equal to longest word length from the phrase.

Input string: #Voi#da#bacu#la#info#

Expected output string: #Voi#da#ucab#la#ofni#

I observed that if I replace this condition:

while (position < sentenceLength - 1) { ... }

With this one:

while (position < strlen(sentence) - 1) { .... }

The while loop is not executed. Why is that?

#define _CRT_SECURE_NO_WARNINGS

#include<iostream>
#include<cstring>

using namespace std;

int main() {
    char sentence[101];
    cin.getline(sentence, 101);
    
    int sentenceLength = strlen(sentence);
    
    char sentenceCopy[101];
    strcpy(sentenceCopy, sentence);
    
    int maxLength = -1;
    char* wordPtr = strtok(sentenceCopy, "#");
    
    while (wordPtr != NULL) {
        int wordLength = strlen(wordPtr);
        if (wordLength > maxLength) {
            maxLength = wordLength;
        }

        wordPtr = strtok(NULL, "#");
    }
    
    int position = -1;
    
    while (position < sentenceLength - 1) {
        position++;
        
        if (sentence[position] == '#') {
            continue;
        }
        
        int currentLength = 0;
        int left = position;
        int right = position;
        
        while (sentence[right] != '#' && right < sentenceLength) {
            right++;
            currentLength++;
        }
        
        position = right;
        right--;
        
        if (currentLength == maxLength) {
            while (left < right) {
                char temp = sentence[left];
                sentence[left] = sentence[right];
                sentence[right] = temp;

                left++;
                right--;
            }
        }
    }
    
    cout << sentence;

    return 0;
}
12
  • It sounds like you may need to learn how to use a debugger to step through your code. With a good debugger, you can execute your program line by line and see where it is deviating from what you expect. This is an essential tool if you are going to do any programming. Further reading: How to debug small programs and Debugging Guide Commented Apr 6 at 20:15
  • I already stepped through the code with the debugger. Every variable has the expected value. Commented Apr 6 at 20:21
  • 2
    Consider the fact that strlen returns a size_t, which is unsigned. Commented Apr 6 at 20:23
  • 1
    sentenceLength is an int. strlen is unsigned. Commented Apr 6 at 21:05
  • 2
    @Mihai BTW, your code is much simpler if you actually used idiomatic C++ instead of C coding. See this. Commented Apr 6 at 21:20

2 Answers 2

2

int position = -1; while (position < strlen(sentence) - 1) { is first like
int position = -1; while ((size_t) -1 < strlen(sentence) - 1) { which is
int position = -1; while (SIZE_MAX < strlen(sentence) - 1) {.

Since SIZE_MAX < strlen(sentence) - 1 is always false, loop does not execute.

Consider using size_t for array sizing and indexing.

Sign up to request clarification or add additional context in comments.

Comments

0

The problem is that you are incorrectly using the signed type int instead of the unsigned type size_t - the returned type of the function strlen.

As result in the condition of this while loop

    int position = -1;
    
    while (position < sentenceLength - 1) {

there is used the usual arithmetic conversions that convert the operand position having the type int to the type of the right operand having the type size_t and due to this conversion of the left operand the value -1 is converted to a very big unsigned integer value that in any case is greater than the value of the right operand.

Try this statement

std::cout << static_cast<size_t>( -1 ) << '\n';

If your compiler supports the C++20 Standard then you could use standard function std::cmp_less declared in header <utility> like

    int position = -1;
    
    while ( std::cmp_less( position, sentenceLength -1 ) ) {

But in any case it is better to use the type size_t instead of the type int when you deal with string lengths.

Also an additional array (and moreover calls of the function strtok) is not required to perform the task. In general that makes the code unsafe.

You could use standard C string functions strspn and strcspn.

Here is a demonstration program.

#include <iostream>
#include <cstring>
#include <algorithm>

char * reverse_max_words( char *s, const char *delimiter = "#" )
{
    size_t max_n = 0;
    
    size_t n = 0;

    for ( const char *current = s; ( n = strspn( current, delimiter ), current[n] != '\0' ); )
    {
        current += n;
        
        n = strcspn( current, delimiter );
        
        if ( max_n < n ) max_n = n;
        
        current += n;
    }
    
    if ( not ( max_n < 2 ) )
    {
        for ( char *current = s; ( n = strspn( current, delimiter ), current[n] != '\0' ); )
        {
            current += n;
            
            n = strcspn( current, delimiter );
            
            if ( n == max_n ) std::reverse( current, current + n );
            
            current += n;
            
        }
    }
    
    return s;
}

int main() 
{
    char sentence[] = "#Voi#da#bacu#la#info#";
    
    std::cout << reverse_max_words( sentence ) << '\n';
}

The program output is

#Voi#da#ucab#la#ofni#

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.