Tuesday 14 February 2017

print "xay" in place of letter 'a' in string


Write A Program to print string "xay" for every letter a inside provided string


void modified_print(const char *str){
        int i=0;
        while(str[i] != '\0'){
                if(str[i] == 'a'){
                        printf("%s", "xay");
                }else{
                        printf("%c", str[i]);
                }
                i++;
        }
        printf("\n");
}

complete code


#include <stdio.h>

void modified_print(const char *str){
        int i=0;
        while(str[i] != '\0'){
                if(str[i] == 'a'){
                        printf("%s", "xay");
                }else{
                        printf("%c", str[i]);
                }
                i++;
        }
        printf("\n");
}


int main(int argc, char *argv[]){
        if(argc != 2){
                fprintf(stderr, "USAGE: %s <string>\n", argv[0]);
                return -1;
        }
        modified_print(argv[1]);
        return 0;
}

Compilation and Output


rajesh@ideapad:~/Rajesh/Blog/string$ gcc string_printer.c
rajesh@ideapad:~/Rajesh/Blog/string$ ./a.out "apple orange"
xaypple orxaynge

No comments:

Post a Comment