hi frieandz I am chaitanya, I am new to this forum, well I am posting my first thread to this forum, please reply to this with an answer,
I got a string called "helloworld" I want to swap the two words in that string an di want to print it as "wolrdhello",
can any one give me a simple logic to this,
thank you
with regards
chaitanya

Recommended Answers

All 4 Replies

Like This

#include <iostream>
#include <string>
using namespace std;
int main ()
{
	string str="HelloWorld";
        string first=str.substr(0,5);
	string second=str.substr(5,5);
	cout<<str;//Original String
	str=second+first;
	cout<<endl<<str;//Swapped String
	return 0;
}

Thank you sunny, but actually what I want is to swap the string with out using any library functions and that to in C language only
thank you
with regards
chaitanya

but actually what I want is to swap the string with out using any library functions and that to in C language only

Try converting my code to C. If any problems then you can post the code where you are stuck.

swap the string with out using any library functions

you will have to
1. create a temporary buffer large enough to hold the longest string.
2. write your own strcpy() function -- but name it something else to avoid confusion with standard C function.
3. Then use the function you wrote in 2 above to copy the string to a temp buffer and swap them

void mystrcpy(char* to,const char* from)
{
  // copy the string one character at a time
}

int main()
{
   char temp[255];
   char s1[255] = "Hello World";
   char s2[255] = "Another World";
   mystrcpy(temp,s1);
   mystrcpy(s1,s2);
   mystrcpy(s2,temp);   
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.