Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

C++ How is this not splitting my string using a delimeter?

Velpus Captiosus

Well-Known Coder
I have this code:

C++:
#include <string.h>

void splitString(string query,char delimeter)
{
    int i,j=0;

                int end = query.size();

                while(i<end)
                {

                   j = query.find(delimeter,i);

                   cout << query.substr(i,j);



                    i = j;



               }
    
}

In my brain this splits a string.For example if we have the query string "Hello|World|" and the delimeter char 'I' we should get in the output : Hello World.Yet when I am running the program it doesnt!
 
I have this code:

C++:
#include <string.h>

void splitString(string query,char delimeter)
{
    int i,j=0;

                int end = query.size();

                while(i<end)
                {

                   j = query.find(delimeter,i);

                   cout << query.substr(i,j);



                    i = j;



               }
   
}

In my brain this splits a string.For example if we have the query string "Hello|World|" and the delimeter char 'I' we should get in the output : Hello World.Yet when I am running the program it doesnt!
Hi there,
I do believe you're missing a function declaration at the top like this
 

Attachments

  • Screenshot_20231129-070702.png
    Screenshot_20231129-070702.png
    118.2 KB · Views: 5
The loop ends when i>end.
If I understood the code well, then i never gets more than 5 (end is 12) when using the string `Hello|World|".

Create a trace table, and this is what you get:
end​
i​
j​
Output​
12​
0​
0​
5​
5​
Hello​
5​
5​
|Worl​
5​
5​
|Worl​
And that last row keeps on repeating.
 
Added comments with line numbers to make it easier to explain.
C++:
/* 1*/ #include <string.h>
/* 2*/
/* 3*/ void splitString(string query,char delimeter)
/* 4*/ {
/* 5*/     int i,j=0;
/* 6*/     int end = query.size();
/* 7*/     while(i<end)
/* 8*/     {
/* 9*/         j = query.find(delimeter,i);
/*10*/         cout << query.substr(i,j);
/*11*/         i = j;
/*12*/     }
/*13*/ }

In line 9, j is set to 5, because the character is found in position 5 in the string.
In line 11, i is set to j, which is 5.

Back to line 7, 5 (i) is less than 12 (end), so the loop continues.
Line 9, you start searching from 5 (i), so the character is again found at position 5.
Line 11, i is again set to 5.

i is still 5, so still less than 12.
Loop continues infinitely.
 
Added comments with line numbers to make it easier to explain.
C++:
/* 1*/ #include <string.h>
/* 2*/
/* 3*/ void splitString(string query,char delimeter)
/* 4*/ {
/* 5*/     int i,j=0;
/* 6*/     int end = query.size();
/* 7*/     while(i<end)
/* 8*/     {
/* 9*/         j = query.find(delimeter,i);
/*10*/         cout << query.substr(i,j);
/*11*/         i = j;
/*12*/     }
/*13*/ }

In line 9, j is set to 5, because the character is found in position 5 in the string.
In line 11, i is set to j, which is 5.

Back to line 7, 5 (i) is less than 12 (end), so the loop continues.
Line 9, you start searching from 5 (i), so the character is again found at position 5.
Line 11, i is again set to 5.

i is still 5, so still less than 12.
Loop continues infinitely.
But the find() searches after a position.
 

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom