Question:
C programming reverse character array help pls ~?
Siaw
2011-09-20 01:50:29 UTC
Below is the question.

Declare an array (character data type) that contains 6 columns. Initialize the array with the characters A to F during excution time (run time). Then write a program to reverse the content in the array. Example:
Before: A,B,C,D,E,F After: F,E,D,C,B,A
[Note: You are allow to declare ONLY ONE array in the program]
______________________________________…
Below is my coding so far....

#include
#include
#include

main()

{

char arr[6] = {'A','B','C','D','E','F'};

strrev(arr);

printf("Reverse of string is \n%s\n",arr);


return 0;

}
________________________________________________________________________________
What wrong with my coding? Help please ~
Three answers:
jplatt39
2011-09-20 02:50:42 UTC
Read this:



http://www.cplusplus.com/forum/unices/1779/
Parv
2011-09-20 03:56:01 UTC
First of all your char array must have one extra element, NULL, at the last.

And char array can also be initialized as "ABCDEF".



Use like this : char arr[7] = "ABCDEF";. This array contains 7 elements. Six characters are alphabets and last character is NULL which marks the end of array.



Here is the program to reverse the array.



#include

#include



void reverse_array(char *str)

{

int i, j, temp, length;



length = strlen(str);



for(i = 0, j = length-1; i < j; ++i, --j)

{

temp = str[i];

str[i] = str[j];

str[j] = temp;

}

}



int main()

{

char arr[7] = "ABCDEF";



printf("Array is \"%s\"\n", arr);



reverse_array(arr);



printf("Reverse array \"%s\"\n", arr);

}
anonymous
2011-09-20 02:02:28 UTC
int i-=i;

printf("%c",arr[0])

for(;i<=6;i++)

printf("%c ",arr[i]);

for(;i!=0;i--)

printf("%c ",arr[i]);

printf("%c ",arr[5]);


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...