adding to an array

Pages: 12
hi to all,

i have a question. I have the following code to enter a string into an array,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

   char str_in[200];
   char str_out[200];

   strcpy (str_in, this->Edit1->Text.c_str());
   stringEcho(str_in, str_out);
   this->Edit2->Text = str_out;

void stringEcho(char str_inCopy [], char str_outCopy [])
{
   int teller = 0;

   teller = strlen(str_inCopy);
   for (count = 0; count < teller; count++)
   {
      countWord = 0;
      while (str_inCopy[countWord] != ' ' && str_inCopy[countWord] != '\0')
      {
         countWord++;
      }
}


How can I make from the string "hello" the string "hello-ello-llo-lo-o"?

I have added the while loop to ensure the size of a single word...for example if you write hello how are you? then it should come up like hello-ello-llo-lo-o how-ow-w are-re-e you-ou-u.

I thought of calculating the number of elements that the echo (the -ello-llo-lo-o part) exists of, but I'm not sure how to add them then to the input.

I would appreciate your suggestions/comments.

Thanks
PinoyNL
Last edited on
If the input string is a fixed array, you won't be able to concatenate them. Well, you'll need to make sure the array has enough space for it.
The tools here would be strcpy_s() and strcat_s(). Copy the input into the output string (with strcpy) and then use strcat to concatenate the pieces. Obviously, do this inside a for() loop.

Something like
1
2
3
4
5
for(int i=1;i<strlen(str_inCopy);i++) 
{
strcat_s(str_outCopy,sizeof(str_outCopy),"-");
strcat_s(str_outCopy,sizeof(str_outCopy),&str_inCopy[i]);
}

I'm not 100% sure about my strcat_s syntax, but I think that's it. That adds a '-' and then everything in str_inCopy as of the i'th character.
here is one way to do 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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <iostream>
using namespace std;

int main ()

{
	
	char a[200];int f[200]; int b,c,d,w,x; b=c=d=0;//declaring and initializing variables
	
	cout << "input string to 'echo':\n";
	gets(a);
	
	cout << "\n";
	
	for (int w=0;w<=strlen(a)-1;w++)f[w]=a[w];//convert input to integer for use in next loop
	w=-1;
	do 
	
	{ 
	
		w+=1;
		char z[200]=" ";// this initialization needed to performed here after each new word,
						//otherwise the end characters in longer previous words were showing up in the process.
		for (int y=0;f[w]!=32;++y){z[y]=a[w];w+=1;}//separating string into individual words
	
		b=c=0;//resetting inner loop variables for next word
		
		cout << z;//display next word
	
		do 
	
		{
		
			cout << "\n";
			b=c+=1; // setting variables b & c to correct value in next loop iteration
	
			do	
		
			{
		
				cout << z[b];// output remaining variable elements after
							//middle loop removed the next leading character
	
				b+=1;//indexing the character sequence +1
		
			} 
		
			while (b<=strlen(z)-1);//inner do/while loop parameter
		
		} 
	
		while (c<=strlen(z)-2);//middle do/while loop parameter
		
		cout << "\n\n";// space between each new word
		
	}
	
	while (w<=strlen(a)-1);// outer do/while loop parameter
	
	return 0;
	
}


input is 'Hello Fine World'


Hello
ello
llo
lo
o

Fine
ine
ne
e

World
orld
rld
ld
d
Last edited on
thanks a lot for your input, they are great solutions.

what if the string is not only 1 word? for example: "hi how are you doing?"...are both solutions then also working?
@ pinoynl... Try it and find out

@ BettyBoopTS... Are you seriously telling him to use gets()?
I guess I could have used cin for a single word mcleano, and pinoynl, if you already have a solution for separating words it could be inserted into the code, but by itself it wont echo the string of more than one word the way you wanted unfortunately :\
Last edited on
@ mcleano I'm trying to figure it out. It's kinda confusing to get it right.

@ BettyBoopTS I think the while loop in the code I posted seperates the words and put the number of letters in a global int
I didnt try to modify your program pinoynl, but I did modify my code to achieve what you are trying to do. I edited the code tag above., anyhow, good luck :)
Thanks anyway for your help :)

I'm working on it hehe I will post the code when it is finished and working.
Oh thats great, I would like to see it when its done!
I would have gone about this another way, assuming of course it wasn't a requirement to read the string to an array.

use a loop, or recursive function would also accomplish this.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <string>
using namespace std;

int main()
{
   string mystring="Hello";
   unsigned int i;
   
   for (i=0; i<mystring.size(); i++)
   {
      if (i == mystring.size())
         break;
      cout << mystring.substr(i,mystring.size()) << "\n";
   }
   
   cout << endl;
   return 0;
}
I had it more like yours gcampton however using do/while loops when I first did it., single word capabilities. it seemed to work ok. But then this post came back:

what if the string is not only 1 word? for example: "hi how are you doing?"...are both solutions then also working?


so I modified the program to separate the words in the string before they were processed it
looks like the output he was trying to get "Hello-ello-llo-lo-o World-orld-rld-ld-d"
that was kind of fun to figure out.
If I put a newline in 31 instead of the dash "-" and a double newline in 51 the output looks like the output you have, which is a cool way of doing it too(I like it better than the horizontal output). If your wondering why I used do/while loops all through it,, its because I need the practice with them. :-)

Edit: hehe, I changed it to the newline look :)
Last edited on
oh I didn't read the requirements properly... still not too much modification is needed.
hi to all,

I have finished the code. here it is. Give me your comment on it, it might can be done shorter, but I'm not sure..I'm tired of thinking about it already hehe, took me two days to finish it hehe. BTW I haven't figured out yet how to do the ', ' and '!' or '?' in the input

Cheers
PinoyNL

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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106

//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "echo.h"

char str_in[200];
char str_out[200];
char str_copy[200];

int count = 0;
int countWord = 0;
int MaxEcho = 0;

void stringCut(char [], char []);
void stringCopy(char [], int);
int echoCount(int);

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TformEcho *formEcho;
//---------------------------------------------------------------------------
__fastcall TformEcho::TformEcho(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TformEcho::buttonEchoClick(TObject *Sender)
{



   strcpy (str_in, this->Edit1->Text.c_str());
   stringCut(str_in, str_out);
   this->Edit2->Text = str_out;
}

void stringCut(char str_inCopy [], char str_outCopy [])
{
   int teller = 0;
   int w = 0;

   teller = strlen(str_inCopy);
   for (count = 0; count < teller; count++)
   {
      countWord = 0;
      while (str_inCopy[count] != ' ' && str_inCopy[count] != '\0')
      {
         str_copy[w] = str_in[count];
         w++;
         count++;
         countWord++;
      }

      if (str_inCopy[count] == ' ')
      {
         str_inCopy[count] = '\0';
      }

      stringCopy(str_copy, countWord);
      w = 0;
   }
}

void stringCopy(char stringOut[], int inputCount)
{

   char stringCopy[200];
   int e = 0;
   int s = 0;
   int c = 0;

   strcpy(stringCopy, stringOut);

   for (int t = inputCount; t > 0; t--)
   {
      for (s = 0 + c; s < inputCount; s++)
      {
         str_copy[e] = stringCopy[s];
         e++;
      }
      if ((s <= inputCount) && (t > 1))
      {
      str_copy[e] = '-';
      strcat(str_out, str_copy);
      c++;
      }
      else
      {
      str_copy[e] = ' ';
      strcat(str_out, str_copy);
      c++;
      }
      int i = 0;
      while (i <= inputCount)
      {
         str_copy[i] = '\0';
         i++;
      }
      e = 0;
   }
}
Last edited on
hi PinoyNL, the code looks good from what I can see, however I cant compile it with the current compiler library that I have :/ if it works on your computer though, thats great! best of luck!
I would strongly recommend that you don't use #pragma. pragmas are stupid, compiler-specific and unnecessary. I've never once used #pragma. Get rid of it.

What are those pragmas doing, anyway?
What is vcl.h?
Last edited on
@BettyBoopTS
yeah it is running correctly on my computer, thanks!

@chrisname
With #pragma, C++Builder can define the directives it wants without interfering with other compilers that support #pragma. If the compiler doesn't recognize it, it ignores it without any warning or error message. BCB 6 advices me to use the #pragma directive to avoid any trouble compiling the code.

vcl.h is necessary to creat a GUI (graphical user interface).



If the compiler doesn't recognize it, it ignores it without any warning or error message

I know that. I know what pragma does itself, I was asking what those particular pragmas are for.

My point is that because #pragma is compiler-specific you shouldn't use it. Maybe the compiler you use makes good use of the pragmas; but have you tried that code on any other compiler? It might not work as expected.

So again, don't use #pragma.

Edit: also, how do you know that those pragmas do what you think they do in any other given compiler?
Last edited on
they won't work in other compilers, I have tried it. I'm currently working with BCB 6. The #pragma is standard in the console format. Thanks anyway for the advice :)
To comment on the code:

That's really great C code. Had you done it in C++ using std::string, you could've
accomplished the same thing in about 3 lines of code total.
Pages: 12