Home CPSC 305

String Exercise

 

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 reverses a string in place. Recall that passing a string into a function means passing a pointer to a char. After calling the reverse function, the string should be reversed.

You should also write a main function which reads in a string of size up to 100 with scanf, calls your reverse function, and then prints the result with printf.


 

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 Attribution-NonCommercial 4.0 International License.