simple i/o reverse order char

need help with simple i/o scanf/printf char order

Page 1 of 1

8 Replies - 2024 Views - Last Post: 19 May 2009 - 06:55 AM Rate Topic: -----

#1 wildnixon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 10-May 09

simple i/o reverse order char

Posted 17 May 2009 - 09:53 PM

I am at wits end; I have an assignment to have a user input abcd and have it print out dcba. I have used char c, and assigned a=d, b=c, c=b, d=a; what do I do next??? I know it's something simple. I have so much trouble with this. This is simple input/output; nothing more. Where do I go once I have made the assignments and declarations?

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

int main(int argc, char *argv[])
{
char a,b,c,d;
a=d, b=c, c=b, d=a;

printf("enter abcd:\n");
scanf("%c, %c, %c, %c", &d, &c, &b, &a);


system("PAUSE");
return 0;
}

This post has been edited by wildnixon: 17 May 2009 - 09:57 PM


Is This A Good Question/Topic? 0
  • +

Replies To: simple i/o reverse order char

#2 AmitTheInfinity  Icon User is offline

  • C Surfing ∞
  • member icon

Reputation: 117
  • View blog
  • Posts: 1,559
  • Joined: 25-January 07

Re: simple i/o reverse order char

Posted 18 May 2009 - 12:12 AM

I think that assignment is expecting you to use arrays and strings.
Was This Post Helpful? 0
  • +
  • -

#3 wildnixon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 10-May 09

Re: simple i/o reverse order char

Posted 18 May 2009 - 06:32 AM

View PostAmitTheInfinity, on 17 May, 2009 - 11:12 PM, said:

I think that assignment is expecting you to use arrays and strings.

That is what I have seen in other sites that I have gone to for help in looking this problem up, but we haven't been introduced to arrays at this point. How would I manipulate a string to print the reverse order of the input??? Thank you for your help.

This post has been edited by wildnixon: 18 May 2009 - 06:38 AM

Was This Post Helpful? 0
  • +
  • -

#4 Enfinik  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 18-May 09

Re: simple i/o reverse order char

Posted 18 May 2009 - 06:40 AM

Well one of the easiest ways would be to do it with an array. Here is some example code and maybe will give you more or less an idea of how to apply it with a string:

#include <iostream>
#include <string>

using namespace std;

int main( ) 
{
	char letter[ 4 ];


	// User Input:
	cout << "Enter Four Letters " << endl;
	cout << "______________________ " << endl;	
	for( int i = 0; i < 4; i++ ) 
	{
		cout << "Enter a letter: ";
		cin >> letter[ i ];
	}// End of for loop

	// Reverse Output:
	cout << endl << "Reverse Output: ";
	for( int i = 3; i >= 0; i-- ) 
	{
		cout << letter[ i ] << " ";
	}// End of for loop

	cout << endl << endl;

	return 0;
}// End of main




Does this example give you a better understanding of more or less what you are trying to accomplish?
I used an array type code because I am not sure if I am allowed to post code that is DIRECTLY related to your homework assignment on this forum.

Also, a helpful hint would be to think of a string as an array.

This post has been edited by Enfinik: 18 May 2009 - 06:49 AM

Was This Post Helpful? 0
  • +
  • -

#5 Enfinik  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 18-May 09

Re: simple i/o reverse order char

Posted 18 May 2009 - 07:20 AM

I'm going to give another hint, and hopefully this helps even more.

1. Lets remember that a string has many similarities as an array.
2. Lets also remember that C++ has many predefined functions.
3. The predefined functions that you can use to accomplish your specific goal are as follows: getline( ) and .length( )
4. You use the getline function to get the entire string from the user ( white spaces -> inclusive )
a. getline syntax: getline( cin, stringVariableGoesHere )
5. You also want to use the .length function to get the total number of characters in the string which will come in handy
when you write your for loop to output the string characters in reverse. That loop will be extremely similar to the
SECOND for loop that I posted previously.
a. .length syntax: stringVariableGoesHere.length( )
6. Your main driver's structure should then be as follows -> Very Similar to the code that I previously posted.
a. Just know the first for loop is no longer necessary. Replace the first loop with getline ( .. )
b. The second for loop pretty much remains the same EXCEPT when you declare the int .. assign it the value:
stringVariableGoesHere.length( ) - 1

Post any more questions you may have.
However, I hope this helps and was very clear :)
Was This Post Helpful? 0
  • +
  • -

#6 wildnixon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 10-May 09

Re: simple i/o reverse order char

Posted 18 May 2009 - 02:50 PM

Is it the same in C as it is in C++? This is a C class that I am taking. The light is kind of shining through... I am a dim bulb where this stuff is concerned; I wish it weren't so, but my background is severely limited, and I don't think the prerequisites listed for the class are adequate.

This post has been edited by wildnixon: 18 May 2009 - 02:55 PM

Was This Post Helpful? 0
  • +
  • -

#7 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: simple i/o reverse order char

Posted 18 May 2009 - 03:23 PM

View Postwildnixon, on 18 May, 2009 - 01:50 PM, said:

Is it the same in C as it is in C++? This is a C class that I am taking.


No not exactly the same.

The C++ string library makes things a little easier (IMO) but they aren't available to you.
You need to make use of C character arrary strings.
No matter this is not a hard problem (once you see through the confusion).

Have a look at what Enfinik has already provided to you as an example of how things work. You will need to change the C++ specific bits into C but the shape of how things work is all there.

Have a try at the code and get back to us with an example of you using arrays in C.
It needn't be 'the' answer.
Just let us see what makes sense to you and what doesn't.
Does your textbook have some examples on use of arrays?
They may be helpful.
Was This Post Helpful? 0
  • +
  • -

#8 wildnixon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 10-May 09

Re: simple i/o reverse order char

Posted 18 May 2009 - 04:49 PM

I will look up arrays in my text, though they are only mentioned briefly in the sections that we have read that are supposedly relevant for this assignment. It is so frustrating, because as I am reading along, it all makes sense; then, when I go to write and execute code, its all wrong!!! I guess this is where it all begins....Thanks again for all of the help. I will read up more and try to understand this. Then I will post what I come up with.
Was This Post Helpful? 0
  • +
  • -

#9 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: simple i/o reverse order char

Posted 19 May 2009 - 06:55 AM

This is another good reference to supplement your textbook on arrays
http://www.cprogramm.../c/lesson8.html
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1