0

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

3
Contributors
4
Replies
5
Views
11 Years
Discussion Span
Last Post by Ancient Dragon
0

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;
}
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

0

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.

0

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);   
}
This article has been dead for over six months. Start a new discussion instead.
Take the time to help us to help you. Please be thoughtful and detailed and be sure to adhere to our posting rules.