having trouble trying to adding x to every other element

I made hit this far but i need to fig out how to add x to ever other element.
any help would be grateful thank you !

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

//PRINTING CONTENTS OF ARRAY and adding x to every other element
// out put should be A x B x C x D x E x F x G x H x I x J x K x L x M x N x O P x Q x R x S x T x U x V x W x X x Y x Z

   char  xstr[] = "A B C D E F G H I J K L M N O P Q R S T U V W X Y Z";
    char *xp;
    int j;
	

  xp = xstr;

  cout << "PRINTING CONTENTS OF ARRAY AND ADDING X TO EVERY OTHER ELEMENT" << endl
	  << "====================================" << endl;

  //for loop untill null.
  for(j = 0; xp[ j ]; j++)
  {
    cout << (xp[ j ]);
  }

cout << endl;
//

system("PAUSE");
return 0;
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int main()
{
   char  xstr[] = "A B C D E F G H I J K L M N O P Q R S T U V W X Y Z";
   char* xp = xstr;

	while(*xp)
{

     if( *xp == 32)
     {
         *xp=120;
     }

     xp++;
}

cout << xstr << endl;


That might work.
Last edited on
It'd be clearer to just use the characters instead of the decimal equivalents but I am not sure if that solves the problem. The OP seems to indicate that the spaces need to be preserved meaning that the array has to grow dynamically. Is that true? If so, use a vector and insert the x characters. Otherwise dynamically create a c-array using new and copy the data from the old to the new in a loop while adding the x and the extra space.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int main()
{
    char  xstr[] = "A B C D E F G H I J K L M N O P Q R S T U V W X Y Z";
    char* xp = xstr;

    while(*xp)
    {

	if( *xp == ' ')
	{
	    *xp='x';
	}

	xp++;
    }

    cout << xstr << endl;
}
Thank you kempofighter that worked well.
Puh, I see how it is.... :(
lol thank you im sorry i tried yours and messed with it for a few and i couldnt get it to work ...
Thank you for your time though!
Lol, mine is exactly the same as his exept his isn't using ascii codes.

They both should do exactly the same thing.
when i compiled yours it didnt do anything just sat there with a blinking cursor ...

again i thank you though for your help :)
Hmmm, that's funny oghmaosiris' function worked fine for me. I just thought it was clearer using the ascii character directly. I also was unsure of whether you needed to keep the spaces but I'm glad it is working for you.
yeah im not sure why it didnt work i copied and past exactly from the forum and then ran it to see if it would work no luck, but yours worked.
His has a closing brace at the end of his. That's probably what happened.
im trying to get this output .


PRINTING CONTENTS OF ARRAY USING THE MOD Option
=====================================================
Even Numbered Element = 0 Contents of Element within Array is = A
Even Numbered Element = 2 Contents of Element within Array is = C
Even Numbered Element = 4 Contents of Element within Array is = E
Even Numbered Element = 6 Contents of Element within Array is = G
Even Numbered Element = 8 Contents of Element within Array is = I
Even Numbered Element = 10 Contents of Element within Array is = K
Even Numbered Element = 12 Contents of Element within Array is = M
Even Numbered Element = 14 Contents of Element within Array is = O
Even Numbered Element = 16 Contents of Element within Array is = Q
Even Numbered Element = 18 Contents of Element within Array is = S
Even Numbered Element = 20 Contents of Element within Array is = U
Even Numbered Element = 22 Contents of Element within Array is = W
Even Numbered Element = 24 Contents of Element within Array is = Y


here is the code i came up i think i got the letters but i cant get the number to be correspond.


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

#include <iostream>
using std::cin;
using std::cout;
using std::endl;

int main() 
{

 cout << "\n\n\nPRINTING CONTENTS OF ARRAY USING THE MOD Option" << endl
	  << "====================================" << endl;


   char* modpstr[] = { "A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};

   int modnumber = 0;   


   for(i = 0; modpstr[ i ]; i++)
  {

	  cout << "Even Numbered Element = " << modnumber << " Contents of Element within Array is = " << modpstr[i++]<< endl;
  }
	  
  

 
   




system("PAUSE");
return 0;
}






closed account (jwC5fSEw)
FYI guys it isn't advised to just give out the code when somebody asks. summey has shown that he's attempted the problem and he has specific issues, so you should guide him towards the right answer without giving it to him directly.
yeah i really appreciate the help, i do want to learn. i just get stumped i know that giving me the answer wont help me learn.

Well, what you're doing isn't matching up the numbers. And you created an array of pointers, which doesn't make sense with what you're trying to do, imo.

If you mix the first problem you had with this problem, then you can get something that will work.

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
#include <iostream.h>
#include <iomanip.h>
#include <conio.h>
#include <fstream.h>
using std::cin;
using std::cout;
using std::endl;


int main() 
{

 cout << "\n\n\nPRINTING CONTENTS OF ARRAY USING THE MOD Option" << endl
	  << "====================================" << endl;
                                                                                              

   char modpstr[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

   //int modnumber = 0;


   for(int i = 0; i<26; i+=2)
  {

	  cout << "Even Numbered Element = " << i << " Contents of Element within Array is = " << modpstr[i]<< endl;
    

  }

system("PAUSE");

return 0;
}
Last edited on
yeah i was confused my assignment has me doing array pointers etc... so thats what i thought i had to use.

Thank you for your help.
Topic archived. No new replies allowed.