Hi, I have a problem with decrementing a sentence in C#, something like:
This is a sentence
This is a sentenc
This is a senten
This is a sente
This is a sent
... I have no idea how to do it, I have done a diamond etc, which basically just goes : - class Program
-
{
-
static void Main(string[] args)
-
{
-
int F = 20;
-
int Q = 0;
-
int E = 10;
-
int A = 0;
-
for (int C = 0; C <= 10; C++)
-
{
-
for (int D = 0; D <= E; D++)
-
{
-
Console.Write(" ");
-
}
-
E = E - 1;
-
for (int B = 0; B <= A; B++)
-
{
-
Console.Write("*");
-
}
-
A = A + 2;
-
Console.WriteLine();
-
}
-
for (int R = 0; R <= 10; R++)
-
{
-
for (int P = 0; P<= Q; P++)
-
{
-
Console.Write(" ");
-
}
-
Q = Q + 1;
-
for (int G = 0; G <= F; G++)
-
{
-
Console.Write("*");
-
}
-
F = F - 2;
-
Console.WriteLine();
-
}
-
-
-
-
Console.ReadLine();
-
}
-
}
-
}
So I thought it might be around the same lines as this, but I cant get anything to work... I am new to C# so trying to figure these things out :/
3 2198 PsychoCoder 465
Recognized Expert Moderator Contributor
Why dont we start with the code you've tried. Sometimes a fresh set of eyes can make a difference.
@PsychoCoder
Ok well I have gone and kind of messed it up now by trying a char array, so this is the coding I have now 0_O
class Program
{
static void Main(string[] args)
{
char[] sin = "This is a sentence".ToCha rArray();
for(int i = sin.Length; i >= 0; --i)
{
Console.Write(s in[i]);
}
}
}
}
To simply take of 1 character at a time, you can use the Substring function which creates a new string from your initial string, but in this case you can specify an index. If you put this in a loop and subtract 1 character at a time, I think you get what you want.
This code did it for me. -
static void Main(string[] args)
-
{
-
string line = "This is my sentence";
-
-
Console.WriteLine(line);
-
-
// While the line still has characters in it, loop
-
while (line.Length != 0)
-
{
-
// Substring from the start till the end minus 1 character
-
line = line.Substring(0, (line.Length - 1));
-
-
// Write the line
-
Console.WriteLine(line);
-
}
-
-
// Hold before closing window
-
Console.Read();
-
}
-
Sometimes things may be less complicated as it seems.
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: J. Campbell |
last post by:
Hi everyone. I'm sure that this is common knowledge for many/most
here. However, it was rather illuminating for me (someone learning
c++) and I thought it might be helpful for someone else. I'm sure I'll
get scolded for an OT post, but I think issues of this type should be
of interest to programmers until they get a handle on them.
I was reading some old threads about the relative advantages of using
++i vs i++ (I won't rehash the many...
|
by: pozz |
last post by:
Hi all,
I need to write a simple incrementing/decrementing function like this:
unsigned char
change( unsigned char x, unsigned char min, unsigned char max, signed char d);
x is the value to increase/decrease
min is the minimum value that x can assume
max is the maximum value that x can assume
|
by: mike |
last post by:
Hello, I am trying to write some code to parse a sentence and hyperlink
just the words in it. I used Aaron's code from an earlier question as
a start.
So far, all the code does below is hyperlink everything separated by a
space, which means stuff like "work." "happy." "Well;" "not." from the
sentence become hyperlinks (whereas im interested in just the words
themselves becoming hyperlinks and the punctuation staying
nonhyperlinks). ...
|
by: Shannon Jacobs |
last post by:
The OL tag still allows for a START value, but that is now deprecated.
I've found sound references that suggest the proper technique now is
to control it with a style for the OL in quetion, but I haven't been
able to find the proper reference. What I actually want is an ordered
list that counts down to one. Are negative increments even possible?
Doesn't seem like a ridiculous idea in the real world, but...
(Yes, I also searched these...
|
by: midnightoil |
last post by:
First, thanks for having a look at this post.
I am trying to create a query that shows decrementing account balances. It reads the following fields from my tbl/Activity table:
Customer ID, Week #, Activity Date, Account Balance, Amount Charged
I need to export the results of this query to a MS Excel spreadsheet so that I have the following:
Customer ID, Week #, Activity Date, Account Balance, Amount Charged
001, 40, ...
| |
by: wohast |
last post by:
Hi, I'm stuck at the very beginning of my current assignment. I just need a little help getting started, and then I know how to do everything else.
ask user:
"Please enter your name followed by a positive integer between 1 and 9999 enclosed in parentheses, such as Cupid(1442)."
how do i use substring to take out just the name that they enter, so that it starts at the first letter they enter, and ends at the (?
also
how do i use the...
|
by: Bill Cunningham |
last post by:
I don't know if I'll need pointers for this or not. I wants numbers
10^16. Like a credit card 16 digits of possible 10 numbers, so I guess that
would be 10^16. So I have
int num ;
These are of course declared and not initialized. Now I want to initialize
them all with '\0'. That I'm guessing would involve while ( --). Or maybe
for. Would pointers be involved in this?
With this excercise I would learn working with multi-dimensional
|
by: fellya |
last post by:
Hi,
i don't have enough experience in writing codes in Python but now i'm trying to see how i can start using Python.
I've tried to write a simple program that can display a sentence. now my problem is how to write a code using split function to split that sentence into words then print out each word separately. let me give u an example:
>>>sentence=" My question is to know how to write a code in Python"
then the output of this...
|
by: fellya |
last post by:
Hi,
i don't have enough experience in writing codes in Python but now i'm trying to see how i can start using Python.
I've tried to write a simple program that can display a sentence. now my problem is how to write a code using split function to split that sentence into words then print out each word separately. let me give u an example:
>>>sentence=" My question is to know how to write a code in Python"
then the output of this...
|
by: mike Andrews |
last post by:
I am trying to remove every other letter from a sentence that I input just by using a for loop. This code however just reprints the sentence
def removeLetter(sentence):
index=0
for letter in sentence:
index=index+1
if index%2==0:
letter=""
sentence=sentence+letter
return sentence
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
| |
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |