C Programs (strings)

1. Write a function to store string from stdin dynamically.

if char str[10]; is used, the str can hold upto 10 characters, if user gives only 5 character string, then rest 5 bytes are wasted, if user wants more than 10 character string, it is not possible to store in the array str. Write a function which dynamically takes user input characters and allocate memory according to user entered characters.

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

char *getstring()
{
        char ch;
        char *str = NULL;
        int i = 0;

        do {
                ch = getchar();
                str = (char *)realloc(str, (i+1)*sizeof(ch));
                str[i++] = ch;
        } while (ch != '\n');

        str[i - 1] = 0;

        return str;
}

int main()
{
        char *str;

        printf("enter string:\n");
        str = getstring();

        printf("the entered string is:\n");
        printf("%s\n", str);

        free(str);
}

2. Remove vowels in a given string

Sample input: This is onesandzeroverse.com
output: Ths s nsndzrvrs.cm

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

char *getstring()
{
        char ch;
        char *str = NULL;
        int i = 0;

        do {
                ch = getchar();
                str = (char *)realloc(str, (i+1)*sizeof(ch));
                str[i++] = ch;
        } while (ch != '\n');

        str[i - 1] = 0;

        return str;
}

int is_vowel(char ch)
{
        switch (ch) {
                case 'a':
                case 'e':
                case 'i':
                case 'o':
                case 'u':
                case 'A':
                case 'E':
                case 'I':
                case 'O':
                case 'U':
                        return 1;
                default:
                        return 0;
        }
        return 0;
}

int main()
{
        char *str;
        int i;

        printf("enter a string:");
        str = getstring();

        for (i = 0; str[i]; i++) {
                if (is_vowel(str[i])) {
                        memcpy(&str[i], &str[i + 1], strlen(&str[i + 1]) + 1);
                        i--;
                }
        }
        printf("the string after removing vowels:\n");
        printf("%s\n", str);
        free(str);
}

input:

practicing C programs

output:

prctcng C prgrms

3. Convert the case of characters for a given sentence

sample input: Convert the CASE
output: cONVERT THE case

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

char *getstring()
{
        char ch;
        char *str = NULL;
        int i = 0;

        do {
                ch = getchar();
                str = (char *)realloc(str, (i+1)*sizeof(ch));
                str[i++] = ch;
        } while (ch != '\n');

        str[i - 1] = 0;

        return str;
}

void convert_case(char *str)
{
        int i;

        for (i = 0; str[i]; i++) {
                if (str[i] >= 65 && str[i] < 90)
                        str[i] = str[i] + 'a' - 'A';
                else if (str[i] >= 97 && str[i] < 122)
                        str[i] = str[i] - ('a' - 'A');
        }
}

int main()
{
        char *str;

        printf("enter string:\n");
        str = getstring();

        convert_case(str);
        printf("The string after conversion:\n");
        printf("%s\n", str);

        free(str);
}

input:

ELON MUSK is CEO of SpaceX

output:

elon musk IS ceo OF sPACEx

4. Reverse the given sentence or string

Sample input: String Programs
output: smargorP gnirtS

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

char *getstring()
{
        char ch;
        char *str = NULL;
        int i = 0;

        do {
                ch = getchar();
                str = (char *)realloc(str, (i+1)*sizeof(ch));
                str[i++] = ch;
        } while (ch != '\n');

        str[i - 1] = 0;

        return str;
}



int main()
{
        char *str;
        int i, l;
        int tmp;

        printf("enter a string:");
        str = getstring();

        l = strlen(str);
        for (i = 0; i < l/2; i++) {
                tmp = str[i];
                str[i] = str[l-i-1];
                str[l-i-1] = tmp;
        }
        printf("string after reversal:\n");
        printf("%s\n", str);
}

input:

reverse this sentence

output:

ecnetnes siht esrever

5. Reverse words in the given sentence

Sample input: Welcome to onesandzeroverse.com
output: onesandzeroverse.com to Welcome

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

char *getstring()
{
        char ch;
        char *str = NULL;
        int i = 0;

        do {
                ch = getchar();
                str = (char *)realloc(str, (i+1)*sizeof(ch));
                str[i++] = ch;
        } while (ch != '\n');

        str[i - 1] = 0;

        return str;
}


int main()
{
        char *str;
        int i;

        printf("enter string:");
        str = getstring();

        i = strlen(str) - 1;
        while (i >= 0) {
                if (str[i] != ' ') {
                        i--;
                        continue;
                }
                printf("%s ", &str[i + 1]);
                str[i] = 0;
                i--;
        }
        printf("%s\n", str);
}

input:

C C++ java python are different types of programming languages

output:

languages programming of types different are python java C++ C

6. Replace a selected word with a given word for a given sentence

input: i am interested in embedded systems
replace word: interested
new word: fascinated
output: i am fascinated in embedded systems

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

char *getstring()
{
        char ch;
        char *str = NULL;
        int i = 0;

        do {
                ch = getchar();
                str = (char *)realloc(str, (i+1)*sizeof(ch));
                str[i++] = ch;
        } while (ch != '\n');

        str[i - 1] = 0;

        return str;
}

int main()
{
        char *str, *word1, *word2;
        char *ptr;
        int len1, len2, found = 0;
        int cmp;

        printf("Enter sentence: ");
        str = getstring();

        //printf("%s\n", str);
        printf("enter word to replace: ");
        word1 = getstring();
        printf("Enter new word: ");
        word2 = getstring();

        len1 = strlen(word1);
        len2 = strlen(word2);

        for (ptr = str; *ptr; ptr++) {
                if (strncmp(word1, ptr, len1) == 0) {
                        found = 1;
                        if (ptr[len1] == ' ' || ptr[len1] == '\0') {
                                if (len1 == len2) {
                                        memcpy(ptr, word2, len2);
                                } else if (len1 > len2) {
                                        memcpy(ptr, word2, len2);
                                        memcpy(ptr + len2, ptr + len1,
                                                strlen(ptr + len1) + 1);
                                } else {
                                        int tmp = ptr - str;
                                        str = realloc(str, strlen(str) + 1 + len2 - len1);
                                        ptr = str + tmp;
                                        /* memove is required here to avoid memory over write */
                                        memmove(ptr + len2 - len1, ptr, strlen(ptr) + 1);
                                        memcpy(ptr, word2, len2);
                                }

                        }
                }
        }
        if (found == 1)
                printf("modified sentence:\n%s\n", str);
        else   
                printf("entered word not found\n");

}

input:

Enter sentence: you are at onzndzerovrse.com
enter word to replace: onzndzerovrse.com
Enter new word: onesandzeroverse.com

output:

you are at onesandzeroverse.com