Home CPSC 305

String Reversal

 

Objective

To get practice writing C programs, including strings.
 

Compiling C Programs

To compile a C program on a Linux system, use the gcc command. This command takes a C program as its argument and compiles it to an executable program called, by default, a.out:

$ gcc hello.c
$ ./a.out
Hello World!

If you want the program to be something other than a.out, it can be specified after the -o flag:

$ gcc hello.c -o hello
$ ./hello
Hello World!

 

Task

For this program you will write a function which prints a string backwards. Recall that passing a string into a function means passing a pointer to a char. Inside this function you should loop through the string backwards, printing out each individual character.

You should also write a main function which reads in a string of size up to 100 with scanf, and then calls your function to print it out backwards.


 

Sample Run

$ ./reverse
Enter a string: abcd
dcba
$ ./reverse
Enter a string: abcde
edcba

 

Submitting

When you are done, please submit the C code under the assignment in Canvas.

Copyright © 2024 Ian Finlayson | Licensed under a Creative Commons BY-NC-SA 4.0 License.