C++ Program to exchange the positions of strings stored in array using array of pointers.

C++ Program to exchange the positions of strings stored in array using array of pointers.

#include <iostream>
#include <string.h>
using namespace std;

int main()
{ 
 char* names[] = { 
"Yugal",
"Kamal",
"Pankaj",
"Vinay",
"Anil"
};

int len = 0;
len = strlen(names[1]);     //length of string no 2

cout<<"\nOrginally string 2 is ";
cout.write(names[1],len).put('\n');
cout<<"and string 4 is ";
cout.write(names[3],len).put('\n');

//exchange the positions of string 2 and 4
char *t;
t = names[1];
names[1] = names[3];
names[3] = t;

//print exchanged strings
len = strlen(names[1]);
cout<<"Exchanged string 2 is ";
cout.write(names[1],len).put('\n');
cout<<"and string 4 is ";
cout.write(names[3],len).put('\n');
return 0;
}

Output:-

C++ program output

If you have any query then tell me by commenting below.

Summary (सारांश)

C++ programming में pointers का सही इस्तेमाल करना सीखना हर student के लिए बहुत जरूरी है। इस article में हम सीखेंगे कि कैसे एक C++ program की मदद से array of pointers का उपयोग करके strings के positions को आपस में बदला (exchange) जाता है। जब हम C++ में strings के साथ काम करते हैं, तो array of pointers डेटा को बहुत ही आसानी से मैनेज करने में मदद करता है। इस tutorial और PDF में आपको निम्नलिखित मुख्य बातें सीखने को मिलेंगी:

  • Pointers और arrays का बेसिक कॉन्सेप्ट क्या होता है।
  • Strings के addresses को आपस में swap करने का तरीका।
  • C++ में strlen() और cout.write() फंक्शन्स का सही उपयोग।
  • Memory management और pointer manipulation की बेहतरीन समझ।
यह लेख और इसके source code आपकी exam preparation के लिए बेहद उपयोगी हैं। यूनिवर्सिटी या कॉलेज के Computer Science एग्जाम्स और वाइवा (Viva) में अक्सर पॉइंटर्स और स्ट्रिंग्स से जुड़े ऐसे प्रैक्टिकल सवाल पूछे जाते हैं। इसे पढ़कर आप कोडिंग के लॉजिक को आसानी से समझ सकते हैं और अपने प्रोग्रामिंग स्किल्स को मजबूत कर सकते हैं।

1 thought on “C++ Program to exchange the positions of strings stored in array using array of pointers.”

Leave a Comment