Jump to content

Creating a while loop to return sequence of characters between two characters

JBaz01
Go to solution Solved by RandleMcmurphy,
#include <stdio.h>
#include <stdlib.h>

int main() {
    char A = 0, B = 0;
    printf("enter a two values: ");
    scanf("%c%c", &A, &B);
    while(A <= B) printf("%c ", A++);
    return 0;
}

image.png.78c8a46b13cafddc3010c66096a53303.png

Basically I have a task to do which is:

 

Write a C program charseq1.c that

  1. prompts the user to enter a two values;
  2. prints out the sequence of characters between the two values (inclusive). So, for example, if the user entered the characters D then N then the output would be:
    D E F G H I J K L M N
    Note, if the user had entered the characters in the opposite order (N then D) the ouptut would be blank. This implies that the program is counting upwards from the first character.

 

As someone who is completely new to C programming, could anyone show me how this is done? I would be able to create this using a for loop, however I am not familiar at all with the while loop.

 

Thanks

Link to comment
Share on other sites

Link to post
Share on other sites

A for-loop is a more sophisticated while-loop. So if you can do this in a for-loop, it only a takes a bit of mental gymnastics to make it into a while-loop.

 

Could you give us your implementation in a for-loop, so I can give some hints and such on how you can convert it to using while-loops?

I say this, instead of giving the answer, because it seems like this is a school assignment and me making the assignment for you wouldn't teach you anything.

 

"We're all in this together, might as well be friends" Tom, Toonami.

 

mini eLiXiVy: my open source 65% mechanical PCB, a build log, PCB anatomy and discussing open source licenses: https://linustechtips.com/topic/1366493-elixivy-a-65-mechanical-keyboard-build-log-pcb-anatomy-and-how-i-open-sourced-this-project/

 

mini_cardboard: a 4% keyboard build log and how keyboards workhttps://linustechtips.com/topic/1328547-mini_cardboard-a-4-keyboard-build-log-and-how-keyboards-work/

Link to comment
Share on other sites

Link to post
Share on other sites

18 minutes ago, minibois said:

A for-loop is a more sophisticated while-loop. So if you can do this in a for-loop, it only a takes a bit of mental gymnastics to make it into a while-loop.

 

Could you give us your implementation in a for-loop, so I can give some hints and such on how you can convert it to using while-loops?

I say this, instead of giving the answer, because it seems like this is a school assignment and me making the assignment for you wouldn't teach you anything.

 

Hi,

Thanks for your response. This is indeed for a degree I am undertaking. Please find the for loop below:

#include<stdio.h>
int main()
{
	int i;
	char start,stop;
	printf("enter two letters:");
	scanf("%c %c",&start,&stop);
	for(i=start;i<=stop;i++)
	{
		printf("%c",i);
	}
	return 0;
}

 

Thanks

Link to comment
Share on other sites

Link to post
Share on other sites

First time posting on here, so apologies if there are any errors with the way in which I am posting.

Link to comment
Share on other sites

Link to post
Share on other sites

not sure if you can do this in C, because i'm mostly experienced with java. However, you could try convert the letters into their ascii code. if the 2nd entry is smaller then the first, then they are reversed in order so the output should be blank. 

 

However, if the 2nd entry's ascii code is larger, then they are in the correct order so you can just do it like your solution posted above. 

Link to comment
Share on other sites

Link to post
Share on other sites

9 minutes ago, JBaz01 said:


 

You could basically turn that for-loop into a while-loop, if that code already works:

// Original loop:
for(i=start;i<=stop;i++)
{
	printf("%c",i);
}

// While loop:
while(i<=stop)
{
	i = start;
	printf("%c",i);
    i++;
}

These two loops do the same thing, just one with a for loop, the other a while loop.

For obvious reason, people often prefer for-loops, because they look a lot neater :P

 

The idea is:

- You use a for-loop when you know how many times you need to iterate (like you know you have a set of characters that is x long, so you iterate x amount of times)

- You use a while-loop for when you are not sure how many times it needs to run (like when you need to wait for user input, you're not sure how many times/how long that will take).

 

You could tackle this problem like this:

1. iterate through letters in the list (should be done with a for-loop, could be done with a while-loop)

 

2. Either a letter is the first character you want, or not.

2a. If it is NOT the first character, go to next letter and go to 2.

2b. If it is the first character, save it to the string and go to 3

 

3. Go to the next character and save it to the string. Check if we have reach the end of the list (3a), the end character(3b) or something else(3c).

3a. Have we reached the end of the list? We didn't find the end character. Do whatever you must print out here.

3b. is the next character the end character? Go to 4.

3c. not the end of the list/end character. go to 3.

 

4. the end character has been reached, print out the output.

 

So that's basically what you need to program, in steps.

1. should really be done with a for-loop, can be done with a while loop too

2. can be combined with 1 into a while-loop. So basically:

while(letterToCheck == firstCharacter)
{
// Save it to the list
    // Move on to 3
}

3. is a loop inside of the while loop above and should again realistically be a for-loop.

Could be done as a while-loop too.

while(letterToCheck == firstCharacter)
{
 	string result += letterToCheck;
    letterToCheck = nextLetter;
    while((letterToCheck != endCharacter) && (letterToCheck != null))
    {
 		string result += letterToCheck;
    	letterToCheck = nextLetter;
    }
}

Now you have to kind of make it do resursion.

 

Imma be honest though, this is me coding at 1am... I kind of am losing focus.

Either someone else will swoop in and finish this up (because I'm pretty sure the steps are alright), but I really can't focus enough to produce a decent bit of code.

 

P.S. the code I wrote above is more in a C# style and uses some 'shortcuts' like just saying 'letterToCheck = nextLetter', of course there is some logic behind this needed too.

9 minutes ago, JBaz01 said:

First time posting on here, so apologies if there are any errors with the way in which I am posting.

Just one tip, we have a feature on this forum specifically for posting code. See the top bar (with the different formatting options and such), this icon is for posting code:

image.thumb.png.a2ed0a0933cccc9ccd6482bb2f7767ab.png

Makes it just much easier to read code.

"We're all in this together, might as well be friends" Tom, Toonami.

 

mini eLiXiVy: my open source 65% mechanical PCB, a build log, PCB anatomy and discussing open source licenses: https://linustechtips.com/topic/1366493-elixivy-a-65-mechanical-keyboard-build-log-pcb-anatomy-and-how-i-open-sourced-this-project/

 

mini_cardboard: a 4% keyboard build log and how keyboards workhttps://linustechtips.com/topic/1328547-mini_cardboard-a-4-keyboard-build-log-and-how-keyboards-work/

Link to comment
Share on other sites

Link to post
Share on other sites

#include <stdio.h>
#include <stdlib.h>

int main() {
    char A = 0, B = 0;
    printf("enter a two values: ");
    scanf("%c%c", &A, &B);
    while(A <= B) printf("%c ", A++);
    return 0;
}

image.png.78c8a46b13cafddc3010c66096a53303.png

Link to comment
Share on other sites

Link to post
Share on other sites

3 hours ago, minibois said:

Imma be honest though, this is me coding at 1am... I kind of am losing focus.

Speaking of which, did you make the infinite loop on purpose so that OP had to fix it?

ENCRYPTION IS NOT A CRIME

Link to comment
Share on other sites

Link to post
Share on other sites

9 hours ago, straight_stewie said:

Speaking of which, did you make the infinite loop on purpose so that OP had to fix it?

If I say 'yes', would you believe that? :ph34r:

I shouldn't have included that code, it was me kinda of messing around with code when very tired.

"We're all in this together, might as well be friends" Tom, Toonami.

 

mini eLiXiVy: my open source 65% mechanical PCB, a build log, PCB anatomy and discussing open source licenses: https://linustechtips.com/topic/1366493-elixivy-a-65-mechanical-keyboard-build-log-pcb-anatomy-and-how-i-open-sourced-this-project/

 

mini_cardboard: a 4% keyboard build log and how keyboards workhttps://linustechtips.com/topic/1328547-mini_cardboard-a-4-keyboard-build-log-and-how-keyboards-work/

Link to comment
Share on other sites

Link to post
Share on other sites

9 hours ago, minibois said:

If I say 'yes', would you believe that? 

 

22 hours ago, minibois said:

You could basically turn that for-loop into a while-loop, if that code already works:


// Original loop:
for(i=start;i<=stop;i++)
{
	printf("%c",i);
}

// While loop:
while(i<=stop)
{
	i = start;
	printf("%c",i);
    i++;
}

These two loops do the same thing, just one with a for loop, the other a while loop.

Your while loop only ever exits on the condition that start = stop when the loop is entered.

For OPs reference, here is the fix:

int i = start; // Move this line here
while (i <= stop)
{
  printf("%c ", i);
  i++
}

// More terse, but harder to read (read: bad)
// Also, modifies the start variable. start will always equal stop + 1 when this loop exits.
while (start <= stop)
  printf("%c ", start++)

 

ENCRYPTION IS NOT A CRIME

Link to comment
Share on other sites

Link to post
Share on other sites

Seems this has a solution (also don't know C) but I'm curious, would it be possible to get the index of each letter in the sequence, and print everything from indexD to indexN? Don't know why you'd do this instead of looping, but curious if it's even a method that would be considered.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×