- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - OpenVMS
- >
- C++ SWAP function - help
Operating System - OpenVMS
1752620
Members
4602
Online
108788
Solutions
Forums
Categories
Company
Local Language
юдл
back
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
юдл
back
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Blogs
Information
Community
Resources
Community Language
Language
Forums
Blogs
Go to solution
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-01-2004 02:41 AM
тАО07-01-2004 02:41 AM
Hi every one
I have a problem writting a function which should
be able to output characters which are swapped i.e give then the input 'HELLOW WORLD'
the output should be 'EHLLWO OWLRD' can some one help.
Regards
Bonnie.
I have a problem writting a function which should
be able to output characters which are swapped i.e give then the input 'HELLOW WORLD'
the output should be 'EHLLWO OWLRD' can some one help.
Regards
Bonnie.
Solved! Go to Solution.
3 REPLIES 3
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-01-2004 03:07 AM
тАО07-01-2004 03:07 AM
Re: C++ SWAP function - help
What is the exat problem you are having?
What do you want the swap to do when there is an odd number of characters?
Here is something to get you going.
Works for me (with TWO spaces betweeb HELLOW and WORLD"
#include
swap (char *string_pointer)
{
short temp, *short_pointer;
char c, *p;
p = string_pointer;
short_pointer = (void *) p;
while (*p) {
c = *p++;
if (*p) {
temp = (c << 8) + *p++;
*short_pointer++ = temp;
}
}
}
main() {
char test[]="HELLOW WORLD";
swap(test);
printf ("%s\n", test);
}
$ cc tmp
$ lin tmp
$ run tmp
EHLLWO OWLRD
$
What do you want the swap to do when there is an odd number of characters?
Here is something to get you going.
Works for me (with TWO spaces betweeb HELLOW and WORLD"
#include
swap (char *string_pointer)
{
short temp, *short_pointer;
char c, *p;
p = string_pointer;
short_pointer = (void *) p;
while (*p) {
c = *p++;
if (*p) {
temp = (c << 8) + *p++;
*short_pointer++ = temp;
}
}
}
main() {
char test[]="HELLOW WORLD";
swap(test);
printf ("%s\n", test);
}
$ cc tmp
$ lin tmp
$ run tmp
EHLLWO OWLRD
$
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-01-2004 03:22 AM
тАО07-01-2004 03:22 AM
Re: C++ SWAP function - help
Great thanks for your help. If the sentence is
of odd number the last character is not switched with anything, but simply attached.
Regards
Bonnie.
of odd number the last character is not switched with anything, but simply attached.
Regards
Bonnie.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-01-2004 03:42 AM
тАО07-01-2004 03:42 AM
Solution
Hi Hein,
sorry for disagree another time inthe same week :-o (never happened before!)
Your code is quick but assume short as 16 bit that's is true in current C/C++ but may be false in future (or on other systems).
Also you assume char as 8 bit so your code can't work with wchar (known as unicode).
This code is slower but more secure and more flexible:
swap (char *string_pointer)
{
char c, *p, *p1;
p = string_pointer;
while (*p)
{
p1 = p;
c = *p++;
if (*p)
{
*p1 = *p;
*p = c;
}
}
}
Changing in above code 'char' with 'wchar' function can work using unicode.
Regards
Antonio Vigliotti
sorry for disagree another time inthe same week :-o (never happened before!)
Your code is quick but assume short as 16 bit that's is true in current C/C++ but may be false in future (or on other systems).
Also you assume char as 8 bit so your code can't work with wchar (known as unicode).
This code is slower but more secure and more flexible:
swap (char *string_pointer)
{
char c, *p, *p1;
p = string_pointer;
while (*p)
{
p1 = p;
c = *p++;
if (*p)
{
*p1 = *p;
*p = c;
}
}
}
Changing in above code 'char' with 'wchar' function can work using unicode.
Regards
Antonio Vigliotti
Antonio Maria Vigliotti
The opinions expressed above are the personal opinions of the authors, not of Hewlett Packard Enterprise. By using this site, you accept the Terms of Use and Rules of Participation.
News and Events
Support
© Copyright 2024 Hewlett Packard Enterprise Development LP