473,786 Members | 2,608 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Decrementing a sentence

2 New Member
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 :

Expand|Select|Wrap|Line Numbers
  1. class Program
  2.     {
  3.         static void Main(string[] args)
  4.         {
  5.            int F = 20;
  6.             int Q = 0;
  7.             int E = 10;
  8.             int A = 0;
  9.             for (int C = 0; C <= 10; C++)
  10.             {
  11.                 for (int D = 0; D <= E; D++)
  12.                 {
  13.                     Console.Write(" ");
  14.                 }
  15.                 E = E - 1;
  16.                     for (int B = 0; B <= A; B++)
  17.                     {
  18.                         Console.Write("*");
  19.                     }
  20.                 A = A + 2;
  21.                 Console.WriteLine();
  22.             }
  23.             for (int R = 0; R <= 10; R++)
  24.             {
  25.                 for (int P = 0; P<= Q; P++)
  26.                 {
  27.                     Console.Write(" ");
  28.                 }
  29.                 Q = Q + 1;
  30.                 for (int G = 0; G <= F; G++)
  31.                 {
  32.                     Console.Write("*");
  33.                 }
  34.                 F = F - 2;
  35.                 Console.WriteLine();
  36.             }
  37.  
  38.  
  39.  
  40.                 Console.ReadLine();
  41.         }
  42.     }
  43. }
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 :/
Jun 3 '12 #1
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.
Jun 3 '12 #2
Barend
2 New Member
@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]);
}
}
}
}
Jun 3 '12 #3
Evolution445
63 New Member
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.

Expand|Select|Wrap|Line Numbers
  1.         static void Main(string[] args)
  2.         {
  3.             string line = "This is my sentence";
  4.  
  5.             Console.WriteLine(line);
  6.  
  7.             // While the line still has characters in it, loop
  8.             while (line.Length != 0)
  9.             {
  10.                 // Substring from the start till the end minus 1 character
  11.                 line = line.Substring(0, (line.Length - 1));
  12.  
  13.                 // Write the line
  14.                 Console.WriteLine(line);
  15.             }
  16.  
  17.             // Hold before closing window
  18.             Console.Read();
  19.         }
  20.  
Sometimes things may be less complicated as it seems.
Jun 8 '12 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

6
6876
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...
10
4608
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
6
1973
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). ...
25
6784
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...
1
1419
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, ...
4
6563
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...
82
3708
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
19
66223
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...
1
4354
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...
2
2827
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
0
9650
marktang
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...
0
9497
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,...
0
10363
Oralloy
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...
0
10164
jinu1996
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...
1
10110
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,...
0
9962
tracyyun
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...
0
8992
agi2029
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...
0
6748
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();...
1
4067
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

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.