C String Practice Exercises and Small Projects

These C string assignments are designed for practice after learning character arrays, the null terminator and common string functions. The original Plus2net exercise inventory is preserved below, with legacy/non-standard functions clearly identified.

Basic String Exercises Top ↑

  1. Receive a string and display the same string.
  2. Find the length of a string without using strlen().
  3. Print a string with one blank space between each character.
  4. Print a string vertically, with one character per line.
  5. Print a string in reverse order.

Advanced String Exercises Top ↑

  1. Copy the nth character of a string when the nth position is supplied as a number.
  2. Copy a string without using strcpy().
  3. Search a string for a character and display its position when found.
  4. Search for one string inside another string.
  5. Print a string vertically with one character per line.
  6. Print a string in reverse order.
  7. Truncate or remove content from a chosen position.
  8. Search for a character inside a string and display the position.
  9. Remove the final character when it is a comma (,).
  10. Extract the part of a string between two chosen input characters.
  11. Search for a string inside another string and display the position when found.
  12. Extract the part of a string between two input strings.

Truncate a String at a Chosen Position Top ↑

The original page described this as deleting the string from the nth position. Assigning '\0' at that index actually terminates the string there, so the visible suffix is truncated.

#include <stdio.h>

int main(void) {
    size_t position = 12;
    char text[] = "welcome to C programming";

    if (position < sizeof text) {
        text[position] = '\0';
    }

    printf("%s\n", text);
    return 0;
}

List Every Character Vertically with Its Position Top ↑

#include <stdio.h>

int main(void) {
    char text[] = "welcome to C programming";

    for (size_t i = 0; text[i] != '\0'; i++) {
        printf("i = %zu => %c\n", i, text[i]);
    }

    return 0;
}

Delete One Character and Shift the Remaining String Top ↑

This variation demonstrates a true single-character deletion rather than truncation.

#include <stdio.h>
#include <string.h>

int main(void) {
    char text[] = "welcome to C programming";
    size_t position = 7;
    size_t length = strlen(text);

    if (position < length) {
        for (size_t i = position; i < length; i++) {
            text[i] = text[i + 1];
        }
    }

    printf("%s\n", text);
    return 0;
}

More Practice Questions Top ↑

  1. Join "welcome " and "to C" so the result is Welcome to C. Practice with strcat().
  2. Repeat the append exercise with strncat() so only the required number of characters is appended.
  3. Read a first name and last name and build a full name while practicing strcpy() and concatenation.
  4. Repeat the copy operation without using strcpy().
  5. Find the length of a first name and then the full name with strlen().
  6. Reverse a name. The historical strrev() is non-standard; also practice the portable replacement shown on that page.
  7. Convert an uppercase name to lowercase. The historical strlwr() is non-standard; use the portable alternative for modern C.
  8. Convert a lowercase name to uppercase. The historical strupr() is non-standard; use the portable alternative for modern C.
  9. Find the first occurrence of 'o' in "Welcome to C" with strchr().
  10. Find the last occurrence of 'o' with strrchr().
  11. Read a password twice and compare the two strings with strcmp().
  12. Repeat the comparison ignoring case. Historical strcmpi() is non-standard; review the portable and POSIX alternatives on the case-insensitive comparison page.
  13. Compare the behavior of case-sensitive strcmp() and case-insensitive comparison.
  14. Search for a substring and report where it begins using strstr().
  15. Mask every character of a modifiable string. Historical strset() is non-standard; use the portable replacement on the strset tutorial.

String Tutorial Hub Top ↑

Return to the complete C Strings hub for all string functions, search/parsing pages, legacy-function notes and practice links.





plus2net.com






✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer