Click here to Skip to main content
15,885,182 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to swap two string using pointer
C
void main(){
char a[15]="Hello";
char  b[10]="Sundresh";
char  *ptra=a;
char  *ptrb=b;
char temp[8];
int length,i=0;
printf("Before swapping a=%sand b=%s",a,b);
while(*ptra!='\0'){
ptra++;
length++;
}
--ptra;
while(--length>=0){
temp[length]=(*ptra);
--ptra;
length--;
}
ptra++;
while(*ptra!='\0'){
ptra=ptrb;
printf("%c",*ptra);
if(*ptra || *ptrb=='\0')
break;
ptra++;
ptrb++;
}
\\Here in loop its coming proper  "sundresh"   
printf(" a=%s",a);//(But here why  output  is  coming only  S


What I have tried:

increment pointer variable and using break statement
Posted
Updated 9-Sep-22 4:08am
v2

I think this line is incorrect:
C++
if(*ptra || *ptrb=='\0')
    break;

This means that if ptra points to a character or ptrb points to a null, the loop will break out. I think you probably meant to write:
C++
if(*ptra == '\0' || *ptrb=='\0')
    break;

But even with that change you are not moving characters from a to b.
 
Share this answer
 
Comments
merano99 9-Sep-22 9:45am    
+5
Richard MacCutchan 9-Sep-22 10:16am    
Thanks but you missed the stars.
merano99 11-Sep-22 11:00am    
ups. was not saved. now done.
(If I got you) In order to swap the content of the arrays, you don't need a temporary array, a single character is just enough. Try
C
#include <stdio.h>
int main()
{
  char a[15]="Hello";
  char b[15]="Sundresh";

  printf("Before swapping a='%s' and b='%s'\n",a,b);
  char * pa, *pb;

  for ( pa = a, pb = b; *pa || *pb; ++pa, ++pb)
  {
    char c = *pa;
    *pa = *pb;
    *pb = c;
  }
  *pa = '\0';
  *pb = '\0';
  printf("after swapping a='%s' and b='%s'\n",a,b);
  return 0;
}
 
Share this answer
 
Comments
merano99 9-Sep-22 9:44am    
+5
CPallini 9-Sep-22 10:54am    
Thank you.
C++
while(*ptra!='\0'){
  ptra=ptrb;
  printf("%c",*ptra);
  if(*ptra || *ptrb=='\0')
    break;
  ptra++;
  ptrb++;
}

The instruction ptra=ptrb sets the two pointers to the same value. After that it can only go wrong.
As Palini and Richard had already written, the recognition of the string end is also wrong afterwards and the usual swap does not take place at all.
For swap you can write a function, or use this if the compiler offers it.
Based on the original code, it might then look like this:
C++
for ( ptra = a, ptrb = b; *ptra || *ptrb; ++ptra, ++ptrb) {
  swap(ptra, ptrb);
}
*ptra = '\0';
*ptrb = '\0';

The length of the strings and the temporary string are not needed, the code is useless.
As you can see, the code assumes that both strings are the same length. If not, the longer one will be shortened. It might make sense to check this before the swap and abort if necessary.
 
Share this answer
 
v3
Comments
CPallini 9-Sep-22 10:54am    
As Richard correctly pointed out, 'you missed the stars'.
Difficult to see what you try to do with thus code.

Advice: Learn to indent properly your code, it show its structure and it helps reading and understanding. It also helps spotting structures mistakes.
C++
void main()
	{
		char a[15] = "Hello";
		char b[10] = "Sundresh";
		char *ptra = a;
		char *ptrb = b;
		char temp[8];
		int length, i = 0;
		printf("Before swapping a=%sand b=%s", a, b);
		while (*ptra != '\0')
		{
			ptra++;
			length++;
		}
		--ptra;
		while (--length >= 0)
		{
			temp[length] = (*ptra);
			--ptra;
			length--;
		}
		ptra++;
		while (*ptra != '\0')
		{
			ptra = ptrb;
			printf("%c", *ptra);
			if (*ptra || *ptrb == '\0')
				break;
			ptra++;
			ptrb++;
		}\\
		Here in loop its coming proper "sundresh"
		printf(" a=%s", a);	//(But here why  output  is  coming only  S

Indentation style - Wikipedia[^]
Best C++ Formatter and Beautifier[^]
Online C/C++ Formatter, Indenter and Beautifier – Techie Delight[^]

Professional programmer's editors have this feature and others ones such as parenthesis matching and syntax highlighting.
Notepad++ Home[^]
ultraedit[^]
Enabling Open Innovation & Collaboration | The Eclipse Foundation[^]
-----
Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 
v2
Comments
sundresjh 10-Sep-22 0:00am    
thank you for your advice but i am using gcc compiler in ubuntu os as moving forward to learn embedded system

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900