Help with C++ Char array?
-
1. Write a program that initialises a char array {a, b, c}. Write a loop to print this array out on the screen without the NUL character. 2. Modify the program so that it prints the array out in reverse order (HINT: have one loop that searches for the terminator – then use the count found in a second loop to count backwards from there back to zero) 3. Write a program that reads two strings from the keyboard one after another. The program should then compare each location in the two arrays to see if they contain the same value. If all of the values are the same right through the string the program should print out “SAME” otherwise it should print out “DIFFERENT”. I have this code: int main() { char str[] = "abc"; int i = 0; { while(i < 3) { printf("%c\n", str[i]); i++; } system("pause>nul"); } } That covers question 1. For question 2 I would use: int i = 0; { while(i =0) printf("%c\n", str[i]); i--; But I think my tutor is asking something different here?
-
Answer:
include <stdio.h> int main() { char str[] = "abc"; int i = 0; while(i < 3){ printf("%c", str[i]); i++; } printf("\n"); system("pause>nul"); } That covers question 1. For question 2 I would use: int i = 0; while(str[i++]) ; i-=2; while(i) printf("%c", str[i--]); i--; part 2.............. #include <stdio.h> int main(void){ int i; char str[]="abc"; i=0; while(str[i++]) ;// the while loop stopped when we hit the terminating 0 of the string i--;// but we then added one to i so it is now two past the end of the string // so we decrement i to point to the terminating 0 while(i) printf("%c", str[--i]); // its --i so we start with the last valid character of the string // and stop when i is zero (after printing the zeroth character os str[] that is, str[0] printf("\n"); // print the new line return 0; } part three is left as an exercise for the student...
Steve at Yahoo! Answers Visit the source
Related Q & A:
- How to convert a char array into an int in C++?Best solution by Stack Overflow
- How to convert from string to char in C++?Best solution by Stack Overflow
- What would be the best book to Help me learn web design in C# and asp.net?Best solution by Stack Overflow
- Help with c++ grading program.Best solution by Yahoo! Answers
- Can anyone help me with this C++ program?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.