String related program (swapping)

This program is not giving the write output. Please tell me what is wrong.

Q.Write a program using user-defined function which is passed a string and that function should cycle the string.(Do not use any string related functions). E.g.
If the string is : Chetna then it should print as
Chetna, hetnaC, etnaCh, tnaChe,naChet, aChetn

Ans.
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
#include <iostream>
using namespace std;

char swamp(char a[], int x)
{
    for(int k=0; k<=x; k++)
    {
        char l= a[x];  a[x]= a[0];  char p=a[x-1];  a[x-1]=l;
        for(int i=1; i<x-2; i++)
        {
            a[i]=a[i+1];
        }
        a[x-2]=p;
        for(int h=0; h<=x; h++)
        {
            cout<<a[h];
        }
    }
}

int main()
{
    char arr[99]; int len;
    cout<<"Enter the string.\n";
    cin.getline(arr,99);
    for(len=0; len!='\0'; len++)
    swamp(arr,len);
    return(0);
}


Last edited on
First, please use the code tags: http://www.cplusplus.com/articles/jEywvCM9/
They make code samples much more readable. (You can edit your post.)

Your code seems overly complicated.


The task is not quite clear. Is it:

1. Function gets a string and prints all cycled versions.

or

2. Function cycles string by one position only. No print. The main() calls the function multiple times and prints result after each call.
function should get the string and print all the cycled versions !
If so, why does your main() call it multiple times?
so can u please give me a correct code for the above question ?
the required output is also given with the question
What you want to do is move the first element to the back, and 'slide' all the others up one, right?
Also, please, one statement per line (there are exceptions, but you should stick to this)
Your function does too many things. It should do only one task. (e.g. put first character to end, move all others up one spot).
Seperate logic from input/output.
I am a beginner in c++ and i am not able to understand whats wrong.
Please correct my program so that i am able to understand where i am wrong.
Do what bugbyte said; write a function that cycles a string by one character. Nothing more. Then show it.
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
#include <iostream>
using namespace std;

int swamp(char a[], int x)
{
    char l;
    for(int k=0; k<=x; k++)
    {
        l=a[0];

        for(int i=0; i<x; i++)
        {
            a[i]=a[i+1];
        }

        a[x]=l;

        for(int h=0; h<=x; h++)
        {
            cout<<a[h];
        }
        cout<<endl;
    }
}

int main()
{
    char arr[99]; int len;
    cout<<"Enter the string.\n";
    cin.getline(arr,99);
    for(len=0; arr[len]!='\0'; len++)
    swamp(arr,len);
    return(0);
}


I made some changes but still it is not giving the right output.
error: no return statement in function returning non-void [-Werror=return-type]|

just saying...

Here what you should also do:
Input:

abc

output:

a
ba
ab
bca
cab
abc

desired output:

abc
bca
cab

right?
So... how can we fix this... as I said, your function really does a little too much.
But then, here is your main function indented properly:
1
2
3
4
5
6
7
8
9
10
11
int main()
{
    char arr[99]; int len;
    cout<<"Enter the string.\n";
    cin.getline(arr,98);   // leave room for '\0'
    // did we really want to call swamp for each character in our input?
    // probally not. We just wanted to count the characters:
    for(len=0; arr[len]!='\0'; len++)  //;  add semicolon here
        swamp(arr,len);
    return(0);
}

see? Alright we fixed that. But there's still a weird space inbetween our output. Interestingly, it is always after the last letter (and then gets put upfront once of corse).
Is this maybe a bug in cin::getline? Probably not.
So we look at the swamp code. Do you know what 'len' is? Do you know how it is related to the indexing of your array? If len is 5, which indices can we use on our array to access the characters?

len:5
0 1 2 3 4

So, when lets look at first loop:
 
for(int k=0; k<=x; k++)


if x is 5 (length)
k:
0 1 2 3 4 5

But since we start counting at zero, we can only go to 4 (makes a total of 5 iterations).
(unless you want to rinse and repeat the process)
Next loop:
 
for(int i=0; i<x; i++)

This seems right, since we only have the indices 0, 1, 2, 3, 4.
Or does it? Let's look at what is inside the loop:
 
a[i]=a[i+1];

whoops. So this is where our famous space is coming from. During the last iteration we go outside of our bounds, and assign the '\0' to the last character in the array. Let's fix that and only iterate until we reached the one-before-last character. (there are two ways to make the loop work)
Last loop, which prints our array:
 
for(int h=0; h<=x; h++)

We can't really see anything wrong with this, but we know it prints characters which don't belong to our word itself.

I hope you will be able to fix your code.
Last edited on
Thanks for the reply man :D
But I figured it out on my own :p
But still thanks for trying to help :)
Here's the working 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
36
37
38
39
40
41
42
#include <iostream>
 using namespace std;

int swamp(char a[], int x)
{
    cout<<"The changed strings are :\n";  char l;
    for(int h=0; h<=x; h++)
        {
            cout<<a[h];
        }
    cout<<endl;
    for(int k=0; k<x; k++)
    {
        l=a[0];
        for(int i=0; i<x; i++)
        {
            a[i]=a[i+1];
        }

        a[x]=l;

        for(int h=0; h<=x; h++)
        {
            cout<<a[h];
        }
        cout<<endl;
    }
}

int main()
{
    char arr[99]; int len=0;
    cout<<"Enter the string.\n";
    cin.getline(arr,99);
    for(int s=1; arr[s]!='\0'; s++)
    {
       len++;
    }
    swamp(arr,len);
    return(0);
}


You "hacked" your length counting mechanism, which is rather confusing and not pretty either. The things I poined out in my post still aren't solved. What if someone uses their own way to count the length of a string. Try to run this:
1
2
3
4
5
6
7
int main()
{
    char arr[] = "Hello";
    int len = 5;
    swamp(arr,len);
    return(0);
}

And to show that what you are doing is even incorrect, run this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int main()
{
    char arr[99]; int len=0;
    cout<<"Enter the string.\n";
    cin.getline(arr,99);
    for(int s=1; arr[s]!='\0'; s++)
    {
       len++;
    }
    cout<<"you entered: " << len << " characters.\n";
    cout << "you entered: ";
    for (int i = 0; i < len; ++i)
        cout << arr[i];
    return(0);
}
Last edited on
for the first one : length should be kept 4, then it will work.
for the second one :
1
2
3
4
5
6
7
8
9
10
11
12
char arr[99]; int len=0;
    cout<<"Enter the string.\n";
    cin.getline(arr,99);
    for(int s=1; arr[s]!='\0'; s++)
    {
       len++;
    }
    cout<<"you entered: " << len+1 << " characters.\n";
    cout << "you entered: ";
    for (int i = 0; i <=len; ++i)
        cout << arr[i];
    return(0);


this will work and give the correct answer.
I know its messy and could be improved but then, I will have to make some changes in my function which will need some more time.
That is why I changed the main and not the function
Well, yes it "works", however, it is false saying that "Hello" has the length 4 and "Foobar" has the length 5, because they don't and if you want to become a good programmer, you should do it right. If I want to port this code to use std::string and I check the length with std::string::size() it will blow up.
for(int p=0;p<y;p+2)

Can you please also tell me if this for loop is possible ?
It's legal, assuming you've already declared y as a suitable type. However, the third statement p+2 doesn't actually do anything. Did you mean to write p = p+2 ?
yeah, i want to increase it by 2.

This is the whole program...
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
#include<iostream>
   using namespace std;

double AltPos(double a[99],int y)
{
    double alt;
    for(int p=0;p<y;p+2)
    {
        alt=a[p];
        a[p]=a[p+1];
        a[p+1]=alt;
    }
    cout<<"The new array after changing alternate positions is :\n";
    for(int g=0;g<y;g++)
    {
        cout<<a[g];
    }
}

int main()
{
    int x; double numbers[x]; 
    cout<<"Enter the size of the array. (It should be an even number)\n";
    cin>>x;
    cout<<"Enter its elements.\n";
    for(int i=0;i<x;i++)
    {
        cin>>numbers[i];
    }
    AltPos(numbers,x);
    return(0);
}


Example when this code runs : If the array initially contains
2,15,3,14,7,9,19,6,1,10 then after rearranging the array should contain
15,2,14,3,9,7,6,19,10,1

Also when i run this program in codeblocks, it force closes saying "awd.exe has stop working". :/
It is not even running the main properly.
Last edited on
If you want to learn Programming in C++ get a good book. You simply can't learn it with flawed online tutorials and from posting threads:
http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list
actually i missed a lot of my classes at school
that is why i am having some problems
but no need now
this one is also complete now
Last edited on
int x; double numbers[x];

This is illegal. The length of the array must be known at the point at which it is created.
Last edited on
Topic archived. No new replies allowed.