PROGRAM TO PRINT ALTERNATE POSITION OF LETTERS IN UPPERCASE:
STATEMENT:
A string S (only alphabets) is passed as input. The printed output should contain alphabets in odd positions in each word in uppercase and alphabets in even positions in each word in lowercase.
PROGRAM:
#include <stdio.h>
int main()
{
char input[100];
fgets(input,100,stdin);
char *ptr = input;
int offset;
char word[100];
while(sscanf(ptr,"%s%n",word,&offset) == 1){
int index=1;
int wordLength=strlen(word);
while(index <= wordLength){
char charToPrint;
if(index%2 != 0){
charToPrint = toupper(word[index-1]);
}else{
charToPrint = tolower(word[index-1]);
}
printf("%c",charToPrint);
index++;
}
printf(" "); //Space after each word.
ptr+=offset;
}
}
Input:
FLoweR iS beauTIFUL
Output:
FlOwEr Is BeAuTiFuL
PROGRAM TO ARRANGE THE ALPHABETS IN THE STRING IN DESCENDING ORDER:
STATEMENT:
A string (with only alphabets) S is passed as input. The program should print the alphabets in the string in descending order. Assume all alphabets will be in lower case.
PROGRAM;
#include<stdio.h>
#include<stdlib.h>
int main()
{
char input[100];
fgets(input,100,stdin);
char *ptr = input;
int present['z'];
char ch='a';
while(ch <= 'z')
{
present[ch] = 0;
ch++;
}
//Now mark alphabets which are present.
while(*ptr != '\0' && *ptr != '\n' && *ptr != '\r')
{
if(*ptr >= 'a' && *ptr <= 'z')
{
present[*ptr]=1;
}
ptr++;
}
//Now print in reverse order
ch = 'z';
while(ch >= 'a')
{
if(present[ch])
{
printf("%c",ch);
}
ch--;
}
}
INPUT/OUTPUT:
If the input is "cake", the output should be "keca"
If the input is "innovation", the output should be "vtonia" (n or o or i should not be repeated)