Hello everyone!
I have a program that is supposed to shift a text to the left by n characters.For example, if I have the text "abcdef" and I shift by 2 characters I wil have "cdef".That is, the first n characters were deleted and the remaining characters where displayed.I managed to make the program but the first version only works if n is smaller than half the characters of the text(word).That is, it won't work for "abcdef" and 4, because 4>6.
This is the code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
|
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
char word[51]; // the word that i want to shift
int n=0; // the number of characters to shift
int i=0;
cout<<"This program shifts the characters of a word to the left(to the beggining)"<<endl;
cout<<"Please enter the word (maximum 50 characters)"<<endl;
cin.get(word,51);
cin.get();
cout<<"Please enter the number of characters you want to shift"<<endl;
cin>>n;
cout<<endl;
if( n<=strlen(word) ) // checks if 'n' is small enough
{
for( i=0;i<=strlen(word) - n;i++ ) // does the actual shifting
{
word[i]=word[i+n];
}
cout<<"The shifted word is: "<<word; // displays the final word
}
else cout<<"You've entered a number larger than the number of caracters in the word"<<endl;
return 0;
}
|
If the word is "abcdef" and n is larger than 3 it won't work.This is frustrating because on paper it works for any number of characters smaller or equal to the total number of characters of the word.
Just o show you that it does work theoretically , here's an example of what hapens for "abcdef" and n=4
1 2 3 4 5 6
|
i=0;i<=2;i++
i=0 word[0]=word[4] => a=e
i=1 word[1]=word[5] => b=f
i=2 word[2]=word[6] => c=NULL
so the final word is: efNULLdef (actually efNULLdefNULL), where NULL is the nul character
because of this cout<<word will display only "ef" because it stops at NULL.
|
It works the same for other n.But, like I said, only theoretically, because in reality it doesn't work for any n < strlen(word), just for n < strlen(word)/2.
To better see what is happening I modified the code a litlle, to display each
character after it has been shifted.Here is the modification:
1 2 3 4 5 6 7 8
|
for( i=0;i<=strlen(word) - n;i++ ) // does the actual shifting
{
word[i]=word[i+n];
cout<<word[i]<<","<<endl; /* this line displays each caracter after shifting just to see
what happens every iteration and is responsible for the garbage data if n>strlen(word)/2 */
}
cout<<endl<<"The shifted word is: "<<word; // displays the final word
|
As you can see i've only modified the code inside the if statement.
This modification produces even a weirder behavior for n> strlen(word)/2, as it returns a lot of garbage data.This is what happens for "abcdef" and n=4:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
|
This program shifts the characters of a word to the left(to the beggining)
Please enter the word (maximum 50 characters)
abcdef
Please enter the number of characters you want to shift
4
e,
f,
,
,
,
,
,
,
,
,
,
,
á,
═,
@,
,
4,
,
",
,
X,
,
",
,
α,
|
The garbage is much bigger but you get the idea.
Please note that for the unmodified version the program gives some error, for this version it also gives garbage data.But this only happens for n> strlen(word)/2.
My question to you is not how to make the program work for any n, I've already done that, but why doesn't this program work for any n if on paper it works fine.That's my question:Why?