943,416 Members | Top Members by Rank

View Poll Results: what is the appropriate coding?
1 0 0%
2 1 100.00%
3 0 0%
4 0 0%
Voters: 1. You may not vote on this poll

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 6548
  • C RSS
Jul 14th, 2004
1

Reversive Array/Integer

Expand Post »
How can i write the coding for a reversive of a given integers.

for example:

char str[] = "012345";

Reverse( str );
printf( str );

then the output will be displayed as "543210"


i need pointer as well to null-terminate string as a parameter and reverses this string. then, the string remains null terminated.

void reverse(char *s)

please help.this is very urgent. thanks.
Similar Threads
Reputation Points: 11
Solved Threads: 0
Newbie Poster
vistatic is offline Offline
2 posts
since Jul 2004
Jul 14th, 2004
1

Re: Reversive Array/Integer

_strrev, _wcsrev, _mbsrev
Reverse characters of a string.

char *_strrev( char *string );

wchar_t *_wcsrev( wchar_t *string );

unsigned char *_mbsrev( unsigned char *string );

Routine Required Header Compatibility
_strrev <string.h> Win 95, Win NT
_wcsrev <string.h> or <wchar.h> Win 95, Win NT
_mbsrev <mbstring.h> Win 95, Win NT

Libraries

LIBC.LIB Single thread static library, retail version
LIBCMT.LIB Multithread static library, retail version
MSVCRT.LIB Import library for MSVCRT.DLL, retail version


Return Value

Each of these functions returns a pointer to the altered string. No return value is reserved to indicate an error.

Parameter

string

Null-terminated string to reverse

Remarks

The _strrev function reverses the order of the characters in string. The terminating null character remains in place. _wcsrev and _mbsrev are wide-character and multibyte-character versions of _strrev. The arguments and return value of _wcsrev are wide-character strings; those of _mbsrev are multibyte-character strings. For _mbsrev, the order of bytes in each multibyte character in string is not changed. These three functions behave identically otherwise.

Security Note These functions incur a potential threat brought about by a buffer overrun problem. Buffer overrun problems are a frequent method of system attack, resulting in an unwarranted elevation of privilege. For more information, see Avoiding Buffer Overruns.
Generic-Text Routine Mappings
  1. TCHAR.H Routine _UNICODE & _MBCS Not Defined _MBCS Defined _UNICODE Defined
  2. _tcsrev _strrev _mbsrev _wcsrev
  3.  
  4.  
  5. Example
  6.  
  7. /* STRREV.C: This program checks an input string to
  8.  * see whether it is a palindrome: that is, whether
  9.  * it reads the same forward and backward.
  10.  */
  11.  
  12. #include <string.h>
  13. #include <stdio.h>
  14.  
  15. void main( void )
  16. {
  17. char string[100];
  18. int result;
  19.  
  20. printf( "Input a string and I will tell you if it is a palindrome:\n" );
  21. gets( string );
  22.  
  23. /* Reverse string and compare (ignore case): */
  24. result = _stricmp( string, _strrev( _strdup( string ) ) );
  25. if( result == 0 )
  26. printf( "The string \"%s\" is a palindrome\n\n", string );
  27. else
  28. printf( "The string \"%s\" is not a palindrome\n\n", string );
  29. }
  30.  
  31.  
  32. Output
  33.  
  34. Input a string and I will tell you if it is a palindrome:
  35. Able was I ere I saw Elba
  36. The string "Able was I ere I saw Elba" is a palindrome
if u want something else .. i will attach masteringstring.cpp ...
Team Colleague
Reputation Points: 55
Solved Threads: 3
Junior Poster
meabed is offline Offline
139 posts
since May 2004
Jul 14th, 2004
0

Re: Reversive Array/Integer

Obfuscated for the sake of being too homework-sounding with no work presented.
#include <stdio.h>
 
char *f(char*s){char*t=s,*u=t;while(*u)++u;--u;
while(t<u){char x=*t;*t++=*u;*u--=x;}return s;}
 
int main ( void )
{
   char text[] = "The quick brown fox";
   puts ( text );
   puts ( f ( text ) );
   return 0;
}
 
/* my output
The quick brown fox
xof nworb kciuq ehT
*/
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Jul 16th, 2004
0

Re: Reversive Array/Integer

Actually i was asking about integers, not string char. by given integers 012345, how can i actually reverse it? .....
Reputation Points: 11
Solved Threads: 0
Newbie Poster
vistatic is offline Offline
2 posts
since Jul 2004
Jul 16th, 2004
0

Re: Reversive Array/Integer

Quote originally posted by vistatic ...
Actually i was asking about integers, not string char. by given integers 012345, how can i actually reverse it? .....
Then this is misleading.
Quote originally posted by vistatic ...
i need pointer as well to null-terminate string as a parameter and reverses this string. then, the string remains null terminated.
char str[] = "012345";
Did you think that you couldn't simply change this line?
  1. char text[] = "012345"; /* "The quick brown fox"; */
Did you consider converting an integer to a string, reversing it, and then converting it back to an integer?

But no one is here to do your homework. What coding attempt have you posted? Or do you expect to post homework questions and simply get a correct answer, nicely formatted and commented?
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Jul 19th, 2004
0

Re: Reversive Array/Integer

Quote originally posted by vistatic ...
Actually i was asking about integers, not string char. by given integers 012345, how can i actually reverse it? .....
yeah .. i am sorry for this mistake ..
see this example

Summary
An algorithm that reverses the order of elements in a sequence

Synopsis
  1. #include <algorithm>
  2.  
  3. namespace std {
  4. template <class BidirectionalIterator>
  5. void reverse(BidirectionalIterator start,
  6. BidirectionalIterator finish);
  7. }
Description
The algorithm reverse() reverses the elements in a sequence so that the last element becomes the new first element, and the first element becomes the new last. For each non-negative integer i <= (finish - start)/2, reverse() applies iter_swap() to all pairs of iterators start + I, (finish - I) - 1.

Complexity
reverse() performs exactly (finish - start)/2 swaps.

Example
  1. //
  2. // reverse.cpp
  3. //
  4.  
  5. #include <algorithm> // for reverse, reverse_copy
  6. #include <vector> // for vector
  7. #include <iostream> // for cout, endl
  8. #include <iterator> // for ostream_iterator
  9.  
  10.  
  11.  
  12. int main ()
  13. {
  14. typedef std::vector<int, std::allocator<int> > Vector;
  15. typedef std::ostream_iterator<int, char,
  16. std::char_traits<char> >
  17. Iter;
  18.  
  19. // Initialize a vector with an array of integers.
  20. const Vector::value_type a[] = { 1, 2, 3, 4, 5,
  21. 6, 7, 8, 9, 10 };
  22.  
  23. Vector v (a + 0, a + sizeof a / sizeof *a);
  24.  
  25. // Print out elements in original (sorted) order.
  26. std::cout << "Elements before reverse: \n ";
  27. std::copy (v.begin (), v.end (), Iter (std::cout, " "));
  28.  
  29. // Reverse the ordering.
  30. std::reverse (v.begin (), v.end ());
  31.  
  32. // Print out the reversed elements.
  33. std::cout << "\n\nElements after reverse: \n ";
  34. std::copy (v.begin (), v.end (), Iter (std::cout, " "));
  35.  
  36. std::cout << "\n\nA reverse_copy to cout: \n ";
  37. std::reverse_copy (v.begin (), v.end (),
  38. Iter (std::cout, " "));
  39. std::cout << std::endl;
  40.  
  41. return 0;
  42. }
  43.  
  44.  
  45. Program Output:
  46.  
  47. Elements before reverse:
  48. 1 2 3 4 5 6 7 8 9 10
  49.  
  50. Elements after reverse:
  51. 10 9 8 7 6 5 4 3 2 1
  52.  
  53. A reverse_copy to cout:
  54. 1 2 3 4 5 6 7 8 9 10
if u need more explaination plz PM me..
Team Colleague
Reputation Points: 55
Solved Threads: 3
Junior Poster
meabed is offline Offline
139 posts
since May 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: recursive algorithm
Next Thread in C Forum Timeline: help me wth file





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC