| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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 |
|
#2
|
|||
|
|||
|
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);
}
|
|
#3
|
|||
|
|||
|
Quote:
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. |
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > Swapping String.... |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|