C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old February 16th, 2006, 09:03 AM
jaro jaro is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2005
Posts: 35 jaro User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 6 h 16 m 44 sec
Reputation Power: 4
Send a message via Yahoo to jaro
Swapping String....

hi there!
I've created a swapping function, well actually there are two version of.

swap 1
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>


void str_swap(char s1[], char s2[])
{
char tmp[500];
strcpy(tmp, s1);
strcpy(s1, s2);
strcpy(s2, tmp);

}

int main(void)
{

	char s1[500];
	char s2[500];

	printf("Enter a word for the first string \n");
	//scanf("%s", &s1);   
	fgets(s1, sizeof(s1), stdin);
		if (s1[strlen(s1)-1] == '\n') 
		{   // full input line read
			s1[strlen(s1)-1] = '\0';  // remove the new-line
		}


	printf("Enter a word for the second string\n");
	//scanf("%s", &s2);   
	fgets(s2, sizeof(s2), stdin);
		if (s2[strlen(s2)-1] == '\n') 
		{   
			s2[strlen(s2)-1] = '\0';
		}

	printf("Before swap. %s , %s \n",s1,s2);
	str_swap(s1, s2);
	printf("After  swap. %s , %s \n",s1,s2);

	return 0;
}



and
swap 2
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>


void str_swap(char *s, char *t)
{
char *temp;
temp = s;
s = t;
t = temp;
printf("\n After  Swap \n");
printf("s: %s\n", s);
printf("t: %s\n", t);
}

int main()
{
char *s;
char *t;
char str1[500];
char str2[500];

printf("Enter a word for the first string\n");
//scanf("%s", &str1);
	fgets(str1, sizeof(str1), stdin);
		if (str1[strlen(str1)-1] == '\n') 
		{   // full input line read
			str1[strlen(str1)-1] = '\0';  // remove the new-line
		}


s = &str1;
printf("Enter a word for the second string\n");
//scanf("%s", &str2);
	fgets(str2, sizeof(str2), stdin);
		if (str2[strlen(str2)-1] == '\n') 
		{ 
			str2[strlen(str2)-1] = '\0';
		}


t = &str2;
printf("\n Before Swap \n");
printf("s: %s\n", s);
printf("t: %s\n", t);
str_swap(s, t);
}

note:Swap 2 has a " warning C4047: '=' : 'char *' differs in levels of indirection from 'char (*)[100]' "



ignoring the warning of Swap 2 ( I haven't figure it out how to remove those warning messages ), Swap 1 and Swap 2 works the same.

I'm just wondering which of the two is the good choice when it come to swapping string. And how would the two compare in the long run?

regards,
Jaro

Reply With Quote
  #2  
Old February 16th, 2006, 10:46 AM
r.3volved r.3volved is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2006
Posts: 5 r.3volved User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 6 m 49 sec
Reputation Power: 0
I'm not sure about the validity of this, but give it a shot...(passing the pointer by reference)

Code:
void str_swap(char * &s, char * &t)
{
char * temp;
temp = s;
s = t;
t = temp;
printf("\n After  Swap \n");
printf("s: %s\n", s);
printf("t: %s\n", t);
}

Reply With Quote
  #3  
Old February 16th, 2006, 09:48 PM
Cirus Cirus is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2005
Posts: 276 Cirus User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 11 h 48 m 58 sec
Reputation Power: 4
Quote:
Originally Posted by jaro
hi there!
I've created a swapping function, well actually there are two version of.

swap 1
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>


void str_swap(char s1[], char s2[])
{
char tmp[500];
strcpy(tmp, s1);
strcpy(s1, s2);
strcpy(s2, tmp);

}

int main(void)
{

	char s1[500];
	char s2[500];

	printf("Enter a word for the first string \n");
	//scanf("%s", &s1);   
	fgets(s1, sizeof(s1), stdin);
		if (s1[strlen(s1)-1] == '\n') 
		{   // full input line read
			s1[strlen(s1)-1] = '\0';  // remove the new-line
		}


	printf("Enter a word for the second string\n");
	//scanf("%s", &s2);   
	fgets(s2, sizeof(s2), stdin);
		if (s2[strlen(s2)-1] == '\n') 
		{   
			s2[strlen(s2)-1] = '\0';
		}

	printf("Before swap. %s , %s \n",s1,s2);
	str_swap(s1, s2);
	printf("After  swap. %s , %s \n",s1,s2);

	return 0;
}



and
swap 2
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>


void str_swap(char *s, char *t)
{
char *temp;
temp = s;
s = t;
t = temp;
printf("\n After  Swap \n");
printf("s: %s\n", s);
printf("t: %s\n", t);
}

int main()
{
char *s;
char *t;
char str1[500];
char str2[500];

printf("Enter a word for the first string\n");
//scanf("%s", &str1);
	fgets(str1, sizeof(str1), stdin);
		if (str1[strlen(str1)-1] == '\n') 
		{   // full input line read
			str1[strlen(str1)-1] = '\0';  // remove the new-line
		}


s = &str1;
printf("Enter a word for the second string\n");
//scanf("%s", &str2);
	fgets(str2, sizeof(str2), stdin);
		if (str2[strlen(str2)-1] == '\n') 
		{ 
			str2[strlen(str2)-1] = '\0';
		}


t = &str2;
printf("\n Before Swap \n");
printf("s: %s\n", s);
printf("t: %s\n", t);
str_swap(s, t);
}

note:Swap 2 has a " warning C4047: '=' : 'char *' differs in levels of indirection from 'char (*)[100]' "



ignoring the warning of Swap 2 ( I haven't figure it out how to remove those warning messages ), Swap 1 and Swap 2 works the same.

I'm just wondering which of the two is the good choice when it come to swapping string. And how would the two compare in the long run?

regards,
Jaro



The warning will come. It is becuase for case 2, temp is just a pointer to a character rather than an array of characters. Though an array can be called as a pointer.The array name will act as pointer pointing to its first element only.

In case of *temp to, only the first element of array s1 is being pointed.However, it is a warning and not as an error because, memory elements in an array are contiguous thus, *temp pointer need not worry about the locations of rest of the elements.

To be more specific, in case of *s1 , it has been pre-aasigned memory of 100 blocks and so writing an array as
[b]char s1[100] [\b]is equivalent of saying as
[b]char *s1 = (char*)malloc(100 * sizeof(char))[\b]
Had you declared and allocated memory to *temp pointer, this warning won't come.

In common cases swap2 would work but problems occur in case
a need is felt to change the data in between then if you try to change *temp you will be greeted by error.

Thus it is interest to allocate memory to temp pointer and then free it.

Last edited by Cirus : February 16th, 2006 at 09:51 PM.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Swapping String....


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
Stay green...Green IT