Thursday, July 30, 2009

Plizzzzz help me write das program in C???

write a C program to reverse a character array using pointers????

Plizzzzz help me write das program in C???
/* Here's the entire code [including the main() function]. I have written a generic reverse function which can be used anywhere. */





#include %26lt;stdio.h%26gt;





void reverse ( char str[] )


{


char *ptr = str;


int arrLength = strlen( str );


int i;


char temp[arrLength];





for ( i = (arrLength-1); i %26gt;= 0; i-- )


{


temp[i] = *ptr++;


}





ptr = str;





for( i = 0; i %26lt; arrLength; i++ )


{


*ptr++ = temp[i];


}








}





int main()


{


char str[] = "This will be reversed";


reverse( str );


printf( "%s", str);





}
Reply:One problem - this won't actually compile.You can't use a variable value to declare an array on the stack. Report It

Reply:void reverseString( char array[] ) {


  char *pHead = array;


  char *pTail = pHead + strlen( array );


  char temp;





  while (pHead %26lt; --pTail) {


    temp = *pHead;


    *pHead++ = *pTail;


    *pTail = temp;


  }


} Report It

Reply:no
Reply://assume char[] array variable is array and is of length arrayLength;


int i;


char temp;


for(i=0; i%26lt;arrayLength/2; i++) {


temp = array[i];


array[i] = array[arrayLength-1-i];


array[arrayLength-1-i] = temp;


}
Reply:Hai here the program you asked





//Prog to rev array using pointers


#include%26lt;stdio.h%26gt;


main()


{


int a[10],*i,n,j=1;


printf("Enter the number of elements\n");


scanf("%d",%26amp;n);


printf("Enter the elements\n");


while(j%26lt;=n)


{


scanf("%d",%26amp;a[j]);


j++;


}


i=%26amp;a[n];


printf("The reversed array\n");


while(n!=0)


{


printf("%d\t",*i);


i--;


n--;


}


}
Reply:#include%26lt;string.h%26gt;


#include%26lt;conio.h%26gt;





void main()


{


char *s1,t;


int l;


s1=malloc(100);


scanf("%s",s1);


len=strlen(s1);


for(i=0 ;i%26lt;len;i++)


{


t=*(s+i);


*(s+i)=*(s+len-1);


*(s+len-1)=t;


}


getch();


}


No comments:

Post a Comment