strings
/* 1. You have been given a String S. You need to find and print whether this string is a
palindrome or not. If yes, print"YES" (without quotes), else print "NO" (without quotes) */
#include <stdio.h>
#include <string.h>
int main()
{
int h,l=0,count=0;
char ch[100];
scanf("%s", ch);
h=(strlen(ch)-1);
while(h>l){
if(ch[l++]!=ch[h--]){
count++;
}
}
if(count>0){
printf("NO");
}
else{
printf("YES");
}
return 0;
}
/* 2. Alice wrote a sequence of words in CamelCase as a string of letters, s, having the
following properties: It is a concatenation of one or more words consisting of English
letters., All letters in the first word are lowercase., For each of the subsequent words, the
first letter is uppercase and rest of the letters are lowercase. Given s, print the number of
words in s on a new line.Sample Input: saveChangesInTheEditor Sample Output: 5 */
#include<stdio.h>
int main()
{
char ch[100000],i;
int count=0;
scanf("%s",ch);
for (i=0;ch[i]!='\0';i++)
{
if(ch[i]>='A' && ch[i]<='Z')
count++;
}
printf("%d", count+1);
return 0;
}
/* 3.Given a string, S, of length N that is indexed from 0 to N-1, print its even indexed and
odd indexed characters as 2 space-separated strings on a single line (see the Sample
below for more detail). 0 is an even index */
#include <stdio.h>
#include <string.h>
int main()
{
int i;
char s[10000];
scanf("%s",s);
for(i=0;i<strlen(s);i++)
{
if (i%2 == 0)
{
printf("%c",s[i]);
}
}
printf(" ");
for(i=0;i<strlen(s);i++)
{
if (i%2 != 0)
{
printf("%c",s[i]);
}
}
printf("\n");
return 0;
}
/* 4.Given a string s consisting of words and spaces, return the length of the last word in the
string. A word is a maximal substring consisting of non-space characters only */
#include <stdio.h>
#include <string.h>
int main()
{
char ch[100];
int length=0,i;
scanf("%[^\n]s",ch);
for(i=0;i<strlen(ch)-1;i++)
{
if(ch[i]==' ')
{
break;
}
else
length++;
}
printf("%d",length);
return 0;
}
/* 5.Develop a C program to arrange the given array of strings in sorted order with respect
to length of the string. If more than one string exists with same length, then display the
string with respect to their alphabetical order. Sample Input: Ravi Radha Akbar Amar Zee
Sample Output: Zee Amar Ravi Akbar Radha */
#include <stdio.h>
#include <string.h>
int main()
{
int i,j;
char name[5][10],temp[10];
for (i=0;i<5;i++)
{
scanf("%s",name[i]);
}
for (i=0;i<4;i++)
{
for (j=i+1;j<5;j++)
{
if (strlen(name[i])>strlen(name[j]))
{
strcpy(temp,name[i]);
strcpy(name[i],name[j]);
strcpy(name[j],temp);
}
}
}
for(i=0;i<5;i++)
{
if (strlen(name[i])==strlen(name[i+1]))
{
if (strcmp(name[i],name[i+1])>0)
{
strcpy(temp,name[i]);
strcpy(name[i],name[i+1]);
strcpy(name[i+1],temp);
}
}
}
for (i=0;i<5;i++)
{
printf("%s ",name[i]);
}
return 0;
}
/* 6. A space explorer's ship crashed on Mars! They send a series of SOS messages to Earth
for help. Letters in some of the SOS messages are altered by cosmic radiation during
transmission. Given the signal received by Earth as a string, S, determine how many
letters of the SOS message have been changed by radiation. Example: The original
message was SOSSOS. Two of the message's characters were changed in transit */
#include<stdio.h>
#include<string.h>
int main()
{
char ch[100];
int count=0,f;
scanf("%s",ch);
for(f=0;f<strlen(ch);f=f+3)
{
if(ch[f]!='S')
count++;
if(ch[f+1]!='O')
count++;
if(ch[f+2]!='S')
count++;
}
printf("%d",count);
return 0;
}
Comments
Post a Comment