Monday, April 28, 2025

C Tutorial - 14 - Strings

What are Strings

In C programming, a string is a sequence of characters stored in contiguous memory locations, terminated by a null character ('\0'). Strings are represented as arrays of characters with a null character at the end.

Strings in C are typically declared using the char data type, and are enclosed in double quotes, like this:

char my_string[] = "Hello, world!";

Here, my_string is an array of characters that contains the string "Hello, world!".

Strings in C are commonly used for storing and manipulating textual data. There are many built-in functions in the C standard library that can be used to manipulate strings, such as strlen() for getting the length of a string, strcpy() for copying one string to another, and strcat() for concatenating two strings.

C strings are null-terminated, which means that the null character marks the end of the string. This allows C programs to handle strings of variable length, without having to specify the length of the string explicitly.

Here is an example of how to declare and print a string in C:

#include <stdio.h>

int main() {
    char my_string[] = "Hello, world!";
    printf("%s\n", my_string);
    return 0;
}

This program declares a string called my_string, and prints it to the console using the printf() function and the %s format specifier. The output of this program would be:

Hello, world!

 

C Strings and Individual Characters 

#include <stdio.h>

int main()
{

    char someString[] = "Hack The Planet";

    printf("Some String: %s \n", someString);

    //Indexes
    printf("Value: %c \n", someString[0]);
    printf("Value: %c \n", someString[3]);
    printf("Value: %c \n", someString[7]);

    //Change Char

    someString[0] = 'W';
    printf("Value: %c \n", someString[0]);
    printf("String Size: %d \n", sizeof(someString));

    int x = 0;

    for (x; x < sizeof(someString); x++)
    {
        printf("Char atm: %c \n", someString[x]);
    }

    return 0;
}

Result:

Some String: Hack The Planet
Value: H
Value: k
Value: e
Value: W
String Size: 16
Char atm: W
Char atm: a
Char atm: c
Char atm: k
Char atm:
Char atm: T
Char atm: h
Char atm: e
Char atm:
Char atm: P
Char atm: l
Char atm: a
Char atm: n
Char atm: e
Char atm: t
Char atm:

Process returned 0 (0x0)   execution time : 0.022 s
Press any key to continue.

This C source code demonstrates some basic operations that can be performed on strings.

The #include <stdio.h> line includes the standard input/output library, which contains the printf() function used for printing output to the console.

The int main() function is the entry point of the program, which returns an integer value to the operating system indicating whether the program executed successfully or not.

The program then declares a character array someString and initializes it with the string "Hack The Planet".

The program then uses the printf() function to print the entire string to the console using the format specifier %s.

The program then uses the array indexing operator [] to access individual characters of the string and prints their values to the console using the %c format specifier.

Next, the program modifies the value of the first character in the string by assigning it the value 'W'. The program then prints the new value of the first character to the console.

The program also prints the size of the someString array using the sizeof() operator, which returns the number of bytes occupied by the array in memory.

Finally, the program uses a for loop to iterate over all of the characters in someString and print their values to the console using the %c format specifier.

This program demonstrates how to declare and initialize a string in C, how to access individual characters of a string using array indexing, and how to modify the value of individual characters of a string. It also shows how to determine the size of a string using the sizeof() operator and how to iterate over all of the characters of a string using a for loop.

No comments:

Post a Comment

Tkinter Introduction - Top Widget, Method, Button

First, let's make shure that our tkinter module is working ok with simple  for loop that will spawn 5 instances of blank Tk window .  ...