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.
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.
#include <iostream>
usingnamespace 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
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 :\
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 :)
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. :-)
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
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?
@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?
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 :)