Join Regular Classroom : Visit ClassroomTech

Data Structure – codewindow.in

Related Topics

Data Structure

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

typedef struct {
    char *data;
    int length;
    int capacity;
} StringBuilder;

void StringBuilder_init(StringBuilder *sb) {
    sb->data = NULL;
    sb->length = 0;
    sb->capacity = 0;
}

void StringBuilder_append(StringBuilder *sb, const char *str) {
    int len = strlen(str);
    if (sb->length + len >= sb->capacity) {
        sb->capacity = (sb->length + len) * 2;
        sb->data = realloc(sb->data, sb->capacity);
    }
    strcpy(sb->data + sb->length, str);
    sb->length += len;
}

void StringBuilder_delete(StringBuilder *sb, int start, int count) {
    if (start + count > sb->length) {
        count = sb->length - start;
    }
    memmove(sb->data + start, sb->data + start + count, sb->length - start - count + 1);
    sb->length -= count;
}

int main() {
    StringBuilder sb;
    StringBuilder_init(&sb);
    StringBuilder_append(&sb, "hello");
    StringBuilder_append(&sb, ", world!");
    printf("%s\n", sb.data);
    StringBuilder_delete(&sb, 0, 5);
    printf("%s\n", sb.data);
    free(sb.data);
    return 0;
}

This implementation uses a struct to store the data, length, and capacity of the string builder. The StringBuilder_init function initializes the struct to NULL values. The StringBuilder_append function appends a string to the end of the builder, resizing the builder if necessary. The StringBuilder_delete function deletes a specified number of characters from the builder, starting at a specified index.

In this example, the main function initializes a string builder, appends two strings to it, prints the resulting string, deletes the first five characters of the string, and prints the resulting string again. Finally, the function frees the memory allocated for the builder.

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

void reverse(char* str) {
    int len = strlen(str);
    for (int i = 0; i < len / 2; i++) {
        char temp = str[i];
        str[i] = str[len - 1 - i];
        str[len - 1 - i] = temp;
    }
}

int main() {
    char str[] = "hello world";
    reverse(str);
    printf("%s\n", str);
    return 0;
}

This program declares a character array str, which is initialized to “hello world”. The reverse function takes a pointer to a character array and modifies it in place to reverse the order of its elements. The main function calls reverse with str as an argument and then prints the result to the console. The output of this program is:

dlrow olleh
my_string = 'Hello World!'

or

my_string = "Hello World!"

Both single and double quotes are equivalent in Python, but you should use the same type of quotes to start and end a string.

string1 = "Hello"
string2 = "world"
result = string1 + " " + string2
print(result)   # Output: Hello world
  • Using the join() method:

string1 = "Hello"
string2 = "world"
result = " ".join([string1, string2])
print(result)   # Output: Hello world

Note that the join() method takes a list of strings as an argument, and uses the string it is called on as the delimiter between each item in the list. In this case, we passed ["Hello", "world"] as the list, and " " as the delimiter.

my_string = "Hello, World!"
length = len(my_string)
print(length) # Output: 13

In this example, we first create a string my_string that contains the text “Hello, World!”. We then use the len() function to find the length of the string, which is 13 (including spaces and punctuation). Finally, we print out the result using the print() function.

string1 = "hello"
string2 = "world"
if string1 == string2:
    print("The strings are equal")
else:
    print("The strings are not equal")

Output:

The strings are not equal

Note that Python is case-sensitive, so "hello" and "Hello" are not equal strings.

s = "hello"
reversed_s = s[::-1]
print(reversed_s)  # Output: olleh

In the example above, [::-1] is a slice that starts from the end of the string and goes backwards, effectively reversing the string.

Alternatively, you can use the reversed() function with the join() method to reverse a string:

s = "hello"
reversed_s = "".join(reversed(s))
print(reversed_s)  # Output: olleh

In this example, reversed() returns an iterator that produces the characters of the string in reverse order. The join() method then concatenates these characters into a new string.

string = "Hello World"
if "H" in string:
    print("Found")
else:
    print("Not found")

To check if a string contains a specific substring using the in keyword, you can use the following syntax:

string = "Hello World"
if "lo W" in string:
    print("Found")
else:
    print("Not found")

To find the index of the first occurrence of a specific character or substring in a string, you can use the find() method. The find() method returns the index of the first occurrence of the specified substring, or -1 if the substring is not found.

Here’s an example:

string = "Hello World"
index = string.find("W")
if index != -1:
    print(f"Found at index {index}")
else:
    print("Not found")

This will output Found at index 6 since the substring “W” is found at index 6 in the string “Hello World”.

string.index(substring)

This method returns the index of the first occurrence of the substring in the string. If the substring is not found, it raises a ValueError.

You can also specify the start and end indices of the search by providing additional arguments:

string.index(substring, start, end)

This will search for the substring only within the slice of the string from the start index to the end-1 index. If the substring is not found within that slice, a ValueError is raised.

string[start_index:end_index]

Where start_index is the index of the first character you want to extract and end_index is the index of the last character you want to extract (not including the character at the end_index position).

For example, to extract the substring “world” from the string “Hello world!”, you can use the following code:

string = "Hello world!"
substring = string[6:11]
print(substring)  # Output: "world"

You can also omit the start_index or end_index to extract a substring from the beginning or end of the string, respectively. For example:

string = "Hello world!"
substring1 = string[:5]  # Extracts "Hello"
substring2 = string[6:]  # Extracts "world!"

Note that slicing a string in Python returns a new string and does not modify the original string.

      

Go through our study material. Your Job is awaiting.

Recent Posts
Categories