10/18/08

Strings and Pointers in C - Part 2

This post is the second part of the Strings and Pointers in C so you may consider reading the Strings and Pointers in C - Part 1 first.

#include <stdio.h>
#define N 10
#define M N+N-1

int main(int argc, char* argv[]) {
char s1[N],s2[N],s[M];
char *a=s1;
char *b=s2;
char *c=s;

/*input and output s1 & s2 */
printf("Give First String : ");
scanf("%s",a);
printf("String1 is %s \n",a);

printf("Give Second String: ");
scanf("%s",b);
printf("String2 is %s\n",b);
printf(" -----------------\n");

/* copy s1 and s2 to s */
for(; *a!='\0' ; c++,a++) {
*c=*a;
}
for(; *b!='\0' ; c++,b++) {
*c=*b;
}
*c='\0';
printf("Two strings together:\n");
for(c=s;((c<s+M) && (*c!='\0'));c++) {putchar(*c);}

/* s = s2reversed + s1 reversed */
for(b=s2; *b!='\0' ; b++) {} /* b = end of s2 */
for(a=s1; *a!='\0' ; a++) {} /* a = end of s1 */
for (c=s,b--; b!=s2-1 ; c++,b--) {
*c=*b;
}
for (a--; a!=s1-1 ; a--,c++) {
*c=*a;
}
*c='\0';
printf ("\nTwo strings reversed:\n");
for(c=s;((c<s+M) && (*c!='\0'));c++) {putchar(*c);}
printf("\n -----------------\n");

/*
replace same chars with * and replace
multiple * with one *
*/
a=s1;
b=s2;
c=s;
if (*a==*b){ *c='*';
}
else {
*c=*a;
}
a++;
b++;
c++;
for (; c<s+N-1; c++,a++,b++) {
if(*a=='\0') {*c='\0';}
else {
if (*a==*b) {
if (*(c-1)=='*') {c--;}
else {*c='*';}
}
else {
*c=*a;
}
}
}
*c='\0';
printf("\nSame char = *:\n");
for(c=s;((c<s+N) && (*c!='\0'));c++) {putchar(*c);}
printf("\n -----*END*-----\n");
}


And finally the third implementation.

#include <stdio.h>
#define N 10
#define M N+N-1

int main(int argc, char* argv[]) {
char *s1=(char*)malloc(sizeof(char)*N);
char *s2=(char*)malloc(sizeof(char)*N);
char *s=(char*)malloc(sizeof(char)*M);
char *temp,*temp1,*temp2;

/*input and output s1 & s2 */
printf("Give First String : ");
scanf("%s",s1);
printf("String1 is %s \n",s1);

printf("Give Second String: ");
scanf("%s",s2);
printf("String2 is %s\n",s2);
printf(" -----------------\n");

/* copy s1 and s2 to s */
for(temp=s,temp1=s1; *temp1!='\0' ; temp++,temp1++) {
*temp=*temp1;
}
for(temp=s,temp2=s2; *temp2!='\0' ; temp++,temp2++) {
*temp=*temp2;
}
*temp='\0';
printf("Two strings together:\n");
for(temp=s;((temp<s+M) && (*temp!='\0'));temp++) {
putchar(*temp);
}

/* s = s2reversed + s1 reversed */
for(temp1=s2; *temp1!='\0' ; temp1++) {} /* temp1 = end of s2 */
for(temp2=s1; *temp2!='\0' ; temp2++) {} /* temp2 = end of s1 */
for (temp=s,temp1--; temp1!=s2-1 ; temp++,temp1--) {
*temp=*temp1;
}
for (temp2--; temp2!=s1-1 ; temp2--,temp++) {
*temp=*temp2;
}
*temp='\0';
printf ("\nTwo strings reversed:\n");
for(temp=s;((temp<s+M) && (*temp!='\0'));temp++) {
putchar(*temp);
}
printf("\n -----------------\n");

/*
replace same chars with * and replace
multiple * with one *
*/
temp1=s1;
temp2=s2;
temp=s;
if (*temp1==*temp2){ *temp='*';
}
else {
*temp=*temp1;
}
temp1++;
temp2++;
temp++;
for (; temp<s+N-1; temp++,temp1++,temp2++) {
if(*temp1=='\0') {*temp='\0';}
else {
if (*temp1==*temp2) {
if (*(temp-1)=='*') {temp--;}
else {*temp='*';}
}
else {
*temp=*temp1;
}
}
}
*temp='\0'; //end of s
printf("\nSame char = *:\n");
for(temp=s;((temp<s+N) && (*temp!='\0'));temp++) {
putchar(*temp);
}
printf("\n -----*END*-----\n");
}

Now you can compare the source codes above and make your results.
The output of course is the same for the three programs above.
Here is an output example (playing_with_strings is the executable file after compilation):

>playing_with_strings
Give First String : computer-blog
String1 is computer-blog
Give Second String: -another_blog
String2 is -another_blog
-----------------
Two strings together:
-another_blog
Two strings reversed:
golb_rehtona-golb-r
-----------------
Same char = *:
comput*-*
-----*END*-----
>

I hope you enjoyed this long post and got something from it. Thank you for visiting. I am waiting for your comments/suggestions.

No comments: