0% found this document useful (0 votes)
9 views4 pages

C String Assignment with Dynamic Memory

This document provides 11 programming assignments involving string manipulation in C using dynamic memory allocation. The assignments include accepting a string from the user with multiple spaces and printing it with a single space delimiter, counting the characters and vowels/consonants in a string, reversing a string, replacing spaces with other characters, determining the number of words in a string, replacing substrings, and printing Fibonacci series up to character codes.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views4 pages

C String Assignment with Dynamic Memory

This document provides 11 programming assignments involving string manipulation in C using dynamic memory allocation. The assignments include accepting a string from the user with multiple spaces and printing it with a single space delimiter, counting the characters and vowels/consonants in a string, reversing a string, replacing spaces with other characters, determining the number of words in a string, replacing substrings, and printing Fibonacci series up to character codes.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

C : Assignment on String(Dynamically) Typing is no substitute for thinking

C Language

Assignment on String Part-1


(Using Dynamic Memory Allocation)

TECHNORBIT INFOSYSTEMS Amol Rahane : 9545535857


C : Assignment on String Typing is no substitute for thinking

1. Write a C program to accept string with multiple spaces from user and print as it

is.(Using Dynamic memory allocation)

2. Write a C program to accept string with multiple spaces from user and print it with a

single space as a delimiter.(Using Dynamic memory allocation)

Eg:

Input String:

a. India is_my country

Output String:

India_is_my_country (Consider _ as space)

3. Write a C program to print count number of characters in given string. (Using

Dynamic memory allocation)

4. Write a C program to accept string and print it in the reverse order. (Using Dynamic

memory allocation)

Eg:

Input String: India is my country

Output String: yrtnuoc ym si aidnI

5. Write a C program to count number of vowels and number of consonants in the

given string. (Using Dynamic memory allocation)

TECHNORBIT INFOSYSTEMS Amol Rahane : 9545535857


C : Assignment on String Typing is no substitute for thinking

6. Write a C program to reverse a given string as below. (Using Dynamic memory

allocation)

Eg:

Input String: India is my country

Output String: aidnI si ym yrtnuoc

7. Write a C program to replace space with ‘$’ in given string. (Using Dynamic memory

allocation)

Eg:

Input String: India is my country

Output String: India$is$my$coutry

8. Write a program which accept sentence from user and print number of words from

that sentence. (Using Dynamic memory allocation)

Input String: India_is_my_country

Output: 4

Input String:

a. India is my country (Consider _ as space)

Output: 4

TECHNORBIT INFOSYSTEMS Amol Rahane : 9545535857


C : Assignment on String Typing is no substitute for thinking

9. Write a C program to replace Good names in mail. (Using Dynamic memory allocation)

Eg:

Raw String: Hello GoodName

Input String: India

Output String: Hello India

Input String: Sangamner

Output String: Hello Sangamner

Input String: technOrbit

Output String: Hello technOrbit

10. Write a C program to print all Fibonacci series up to each ASCII code of alphabets in

given string. (Using Dynamic memory allocation)

11. Write a C program which accepts a string from user which contains a characters

from ‘b’ to ‘y’. (Using Dynamic memory allocation)

Eg:

Input String: mn jn kn kazfd

Output String: mn jn kn k

TECHNORBIT INFOSYSTEMS Amol Rahane : 9545535857

Common questions

Powered by AI

Programmers might opt for dynamic memory allocation when handling strings with varying sizes to provide flexibility and manage memory efficiently. Dynamic allocation allows the program to only use the memory required for the actual data being processed, accommodating variably-sized strings without predefining a maximum capacity. This adaptability reduces the likelihood of memory overflow and waste, and helps the program run efficiently across different input sizes .

Challenges in replacing 'Good names' involve correctly allocating memory for different sized inputs and ensuring memory leaks are prevented. Since the replacement string may vary in length, the program must carefully manage memory to accommodate new strings without overwriting data or causing buffer overflows. This can be addressed by initially estimating the maximum length needed and reallocating memory dynamically as needed. Proper error checking after each dynamic operation is also crucial to handle allocation failures gracefully .

To replace spaces with a specific character using dynamic memory allocation in C, you first need to allocate enough memory to hold the input string. Then, traverse the string character by character using a loop. When a space character is encountered, replace it with the specified character, such as '$'. Continue the loop until the end of the string, ensuring to account for the null terminator. Finally, dynamically manage the memory to ensure no leaks occur by freeing the allocated space after use .

The benefits of dynamic memory allocation include flexibility and efficient memory usage, as it allocates memory at runtime based on the program's needs, unlike static allocation which reserves a fixed size at compile time. This can lead to more efficient memory use when dealing with varying string sizes. However, the drawbacks include increased complexity in managing memory, as developers must explicitly allocate and free memory, leading to potential memory leaks if not handled correctly .

Dynamic memory allocation allows a C program to handle strings of variable lengths efficiently without wasting memory by allocating exactly the amount of memory needed at runtime. This is particularly useful when reversing a string or counting vowels and consonants, as the size of the string may not be known until runtime. By using functions like malloc() and realloc() to manage memory, a program can adjust its memory usage based on the actual input string size, leading to efficient memory use .

Dynamic memory allocation facilitates processing Fibonacci series in a string by allowing the program to adjust its memory usage based on the size and content of the string dynamically. For each character in the string, the program calculates its ASCII value and then generates all Fibonacci numbers up to that ASCII value. This process requires varying amounts of memory depending on the character values. Dynamic allocation ensures that only the necessary amount of memory is used, and can be freed or modified as needed, thus optimizing computational resources .

A C program designed to count words in a sentence using dynamic memory allocation should first allocate memory for the input string. It should then iterate over the string, checking for delimiters such as spaces or underscores to identify word boundaries. Each time a delimiter is detected, it increases a word count. By using dynamic memory allocation, the program can handle strings of any size and dynamically adjust memory usage as necessary. This approach ensures flexibility and efficiency, particularly when working with inputs that have varying delimiters .

Dynamic memory allocation enhances string reversal operations by allowing for the handling of strings of any length without predefined limits. With static allocation, a maximum string size must be known in advance, potentially wasting memory on smaller strings or failing with larger ones. Dynamic allocation provides a way to allocate precisely as much memory as required for the actual string length entered by the user, improving memory efficiency and flexibility, crucial for operations like reversing the string .

Robust memory management involves several strategies: using functions like malloc() and realloc() strategically to allocate space only when necessary, and free() to release it as soon as it is no longer needed to avoid leaks. Developers should validate every dynamic memory allocation call to check for successful allocation. Implement exception handling mechanisms to safely handle errors that occur during memory operations, maintaining program stability and efficiency. These precautions ensure reliable memory management when manipulating strings .

The method involves first dynamically allocating memory for the input string. Traverse the string character by character, categorizing each character as a vowel or consonant, and increment counts accordingly. Use a secondary loop to eliminate multiple spaces by checking preceding characters and shifting subsequent characters leftward in the array when consecutive spaces are detected. Memory allocation at the start and careful management of pointers ensure efficiency and correctness, eliminating redundant spaces while counting .

You might also like