Posts

Showing posts with the label STRINGS

There are N students standing in a row and numbered 1 through N from left to right. You are given a string S with length N, where for each valid i, the i-th character of S is 'g' if the i-th student is a girl or 'b' if this student is a boy. Students standing next to each other in the row are friends.

 #include<stdio.h> #include<string.h> int main() {     char students[100001];     int t,j,count=0,length;     scanf("%d",&t);     while(t--){     count=0;     scanf("%s",students);     length=strlen(students);         for(j=0;j<length;j++){             if((students[j]=='g'&&students[j+1]=='b')||(students[j]=='b'&&students[j+1]=='g')){             count++;             j++;}             else             continue;}     printf("%d\n",count);}     return 0; }

Lokesh have been given a String S consisting of uppercase and lowercase English alphabets. Lokesh need to change the case of each alphabet in this String. That is, all the uppercase letters should be converted to lowercase and all the lowercase letters should be converted to uppercase.

#include <stdio.h> #include<string.h> int main() {char ch[100]; int length,i;  scanf("%s",ch); length=strlen(ch); for(i=0;i<length;i++) {   if(ch[i]>=65&&ch[i]<=90)   ch[i]=ch[i]+32;   else if(ch[i]>=97)   ch[i]=ch[i]-32; } printf("%s",ch);                return 0; } 

Lokesh usually likes to play cricket, but now, he is bored of playing it too much, so he is trying new games with strings. Lokesh's friend Tina gave him binary strings S and R, each with length N, and told him to make them identical. However, unlike Tina, Lokesh does not have any superpower and Tina lets Lokesh perform only operations of one type: choose any pair of integers (i,j) such that 1≤i,j≤N and swap the i-th and j-th character of S.

#include <stdio.h> int main() {     char s[100],r[100];     int t,i,N;     scanf("%d",&t);     while(t--)     {         int count1=0,count2=0;         scanf("%d",&N);         scanf("%s%s",s,r);         for(i=0;i<N;i++)         {             if(s[i]=='1') count1++;             if(r[i]=='1') count2++;         }         if(count1==count2)         printf("YES\n");         else         printf("NO\n");     }     return 0; } 

Nathan wants to implement wildcard pattern matching supporting only the wildcard '?'. The wildcard character '?' can be substituted by any single lower case English letter for matching. He has two strings X and Y of equal length, made up of lower case letters and the character '?'. He wants to know whether the strings X and Y can be matched or not.

#include <stdio.h> #include<string.h> #include<ctype.h> int main()   {int MAX=10;  char a[MAX],b[MAX];  int t;  scanf("%d",&t);  while(t--){  scanf("%s",a);  scanf("%s",b);  int i,  n=strlen(b),no=0;  for(i=0;i<n;i++)  {if(isalpha(a[i])&&isalpha(b[i])&&a[i]!=b[i])  no++;}  if(no>0)  printf("No\n");  else printf("Yes\n"); } return 0; } 

Afghanistan has surrounded by attackers. A truck enters the city. The driver claims the load is food and medicine from Pakistanis. Yasir is one of the soldier in Afghanistan. He doubts about the truck, maybe it's from the siege. He knows that a tag is valid if the sum of every two consecutive digits of it is even and its letter is not a vowel. If the tag is invalid then Yasir need to arrest the driver of the truck with invalid tab. If it is valid the truck is allowed inside the country.Can you help yasir in determine if he need to arrest or allow the truck?

 #include <stdio.h> int main() {int n=0,c=0; char tag[9]; scanf("%s",tag); while(n<8){     if(tag[n+1]=='-')     n+=2;     else if((tag[n]+tag[n+1])%2==0)     c++;     n++;} if(c>=4)printf("Allowed"); else printf("Arrest"); return 0; }

Not everyone probably knows that Nivin has younder brother Nithin. Currently Nithin learns to read.He knows some subset of the letter of Latin alphabet. In order to help Nithin to study, Nivin gave him a book with the text consisting of N words. Nithin can read a word iff it consists only of the letters he knows.Now Nivin is curious about which words his brother will be able to read, and which are not. Please help him!

 #include <stdio.h> #include <string.h> int main() {     char string[100];char search[100];     int t,i,j,len,c=0;     scanf("%s",string);     len=strlen(string);     scanf("%d",&t);     while(t- -){         c=0;         scanf("%s",search);         for(i=0;i<=strlen(string)-1;i++)         {             for(j=0;j<=strlen(search)-1;j++)             {                 if(string[i]==search[j]){                 c++;break;}             }         }         if(c==len)         printf("Yes\n");         else         printf("No\n");     }       ...

Elavenil likes strings a lot but she likes palindromic strings even more. Today she found an old string 's' in his garage. The string is so old that some of its characters have faded and are unidentifiable now. Faded characters in the string are represented by '.' whereas other characters are lower case Latin alphabets i.e ['a'-'z'].Elavenil being the palindrome lover decided to construct the lexicographically smallest palindrome by filling each of the faded character ('.') with a lower case Latin alphabet. Can you please help her complete the task?

 #include<stdio.h> #include<string.h> int main() { int t; char pali[500]; scanf("%d",&t); while(t>0) {     int c=0;     int i,j;     scanf("%s", pali);     for (i=0,j=strlen(pali);i<=strlen(pali)/2;i++,j--)        {         if(pali[i]=='.' && pali[j-1]=='.')         {             pali[i] ='a';             pali[j-1] ='a';         }         else if(pali[i]=='.')         pali[i]=pali [j-1];         else if(i==strlen(pali)/2 && pali[i]=='.')         pali[i]='1';         else if(pali[i]!=pali[j-1] && pali[i]!='.' && pali[j-1]!='.')         {             c++;             printf("-1...

Janu and Ram are close friends who task a lot about life.They go though a lot of inspiring "Quotes of Life".One fine day they had a small game.According to the game Ram will Read one of the Quote about life from the book and Jannu have to think a word about life in her mind without disclosing it to Ram.Finally once Ram completed reading the quoted Jannu will say if the word she thought in her mind is there in the Quote read by Ram.Can you convert the same scenario to a programming logic ?If the work thought by Jannu was present in the Quote then you have to print "Exists" else print "Dosen't Exists".

 #include <stdio.h> #include <string.h> int M,N,i,j,res; int main() {int t; scanf("%d",&t); while(t--){  char string[100];  char p[100];  scanf("%s%s",string,p);  M=strlen(p);  N=strlen(string);  res=0;  for(i=0;i<=N-M;i++){  for(j=0;j<M;j++)  if(string[i+j]!=p[j])  break;  if(j==M){  res++;  j=0;  }  }  if(res>0)printf("Exists\n");  else printf("Dosen't Exists\n"); } return 0; }

Vimal has found two very old sheets of paper, each of which originally contained a string of lowercase Latin letters. The strings on both the sheets have equal lengths. However, since the sheets are very old, some letters have become unreadable.Vimal would like to estimate the difference between these strings. Let's assume that the first string is named S1, and the second S2. The unreadable symbols are specified with the question mark symbol '?'. The difference between the strings equals to the number of positions i, such that S1i is not equal to S2i, where S1i and S2i denote the symbol at the i the position in S1 and S2, respectively.Vimal would like to know the minimal and the maximal difference between the two strings, if he changes all unreadable symbols to lowercase Latin letters. Now that you're fully aware of Vimal's programming expertise, you might have guessed that he needs you help solving this problem as well. Go on, help him!

#include<stdio.h> int main() { int t,minimal,maximal,count,i; char S1[101],S2[101]; scanf("%d",&t); while(t--) {scanf("%s %s",S1,S2); i=0; count=minimal=maximal=0; while(S1[i]!='\0') {if(S1[i]=='?'||S2[i]=='?') count++; else if(S1[i]!=S2[i]) minimal++;   i++; } maximal=minimal+count; printf("%d %d\n",minimal,maximal); } return 0; } 

Every day, Selvan goes to his office by train and buys the ticket from the counter on the day of travel. On the ticket, there is a letter-code that is represented as a string of upper-case Latin letters.Selvan believes that the day will be successful in case exactly two different letters in the code alternate. Otherwise, he believes that the day will be unlucky. Please see note section for formal definition of alternating code.If the ticket code is given. Please determine, whether the day will be successful for Selvan or not. Print "Successful Day" or "Unsuccessful Day" (without quotes) corresponding to the situation.

 #include <stdio.h> int main() {   int n;     char ticketnumber[102];     scanf("%d",&n);     for (int i=0;i<n;i++)     {            scanf("%s",ticketnumber);         if(ticketnumber[0]!=ticketnumber[2]||ticketnumber [1]!=ticketnumber[3]||ticketnumber[3]!=ticketnumber [5])         {printf("NO\n");}         else         {printf("YES\n");}     } return 0; }

Fazil's faculty gave him a string S consisting of only 1s and 0s and he need to find the number of substrings which start and end both in 1.In this problem, a substring is defined as a sequence of continuous characters S

 #include <stdio.h> #include<string.h> int main() { char string; int string1,test; scanf("%d",&test); while(test--) { int sum=0,i;     scanf("%d",&string1);     char str[string1];     scanf("%s",str);     for(i=0;i<strlen(str);i++)     if(str[i]=='1') sum++;     int ans=sum*(sum+1)/2;     printf("%d\n",ans); } scanf("%c",&string); return 0; }

Roopa has given a program to her close friend Jhansi in the apartment where she lives.The task is if the number is given it needs to be converted into words.It's like writing the amount in numbers on the cell of the bank challan and then writing it in words.

#include <stdio.h> #include<string.h> int main() {   const char *a[]={ "zero" , "one" , "two" , "three" , "four" , "five" , "six" , "seven" , "eight" , "nine" };     const char *b[]={ "ten" , "eleven" , "twelve" , "thirteen" , "fourteen" , "fifteen" , "sixteen" , "seventeen" , "eighteen" , "nineteen" };     const char *c[]={ " " , " " , "twenty" , "thirty" , "forty" , "fifty" , "sixty" , "seventy" , "eighty" , "ninety" };     //const char *p[]={"hundred" , "thousand" };     char num[10];     int l,n,n1;     scanf("%s",num);     l=strlen(num);     if(l==4)     {       while(l--)      {          if(l==3&&num[0]!='0')       {           p...

Raju the fan of Great Mathematician Ramanujan developed an encoder that encodes the first 16 lowercase English letters using 4 bits each. The first bit (from the left) of the code is 0 if the letter lies among the first 8 letters, else it is 1, signifying that it lies among the last 8 letters.

#include <stdio.h> #include <math.h> int main() {     int t, n, i, j, k, sum=0;     scanf("%d", &t);     for(i=0;i<t;i++){         scanf("%d", &n);         char s[n];         scanf("%s", s);         for(j=0;j<n/4;j++){             for(k=j*4+1;k<j*4+4+1;k++){                 if(s[k-1]=='1') {                     sum = sum + pow(2, 4*j-(k-4));                 }             }         printf("%c", sum+97);         sum = 0;         }         printf("\n");     } return 0; } 

Nathan won the man of the match award in the recently concluded local tournament final. So the friends of nathan have asked him to take them to cinemas as a trat for winning man of the match.But Nathan is short of money to take them to cinemas so to postpone the cinema plan he tred to engage them with the programming challenge. The task was if the string S was given they have to change the string according to the following condition:If the first letter in a string is capital letter then change the full string to capital letters.

 #include <stdio.h> #include<string.h> #include<ctype.h> int main() {     char s[100];     int n,i;     scanf("%d",&n);     for(i=0;i<n;i++)     {         scanf("%s", s);         int j=0;         if(isupper(s[0]))         {             while (s[j]) {             s[j]=toupper(s[j]);             j++;         }         }         if(islower(s[0]))         {             while (s[j]) {             s[j]=tolower(s[j]);             j++;         }         }         printf("%s\n", s);     } return 0; }

Hassan is given a string containing characters A and B only. Your task is to change it into a string such that there are no matching adjacent characters. To do this, you are allowed to delete zero or more characters in the string.

#include<stdio.h> #include<string.h> int main() { char s[100001]; int t,count=0,i; scanf("%d",&t); while(t!=0) {scanf("%s",s); for(i=0;i<strlen(s);i++) {if(s[i]==s[i+1]) count++; } printf("%d\n",count); count=0;    t--;}        return 0;} 

Aaron has a number D containing only digits 0's and 1's. He wants to make the number to have all the digits same. For that, he will change exactly one digit, i.e. from 0 to 1 or from 1 to 0.

 #include <stdio.h> #include<string.h> #include<ctype.h> int main() {     int t,n1,n0,len,i;     char str[100002];     scanf("%d",&t);     while(t--){     scanf("%s",str);     n1=n0=0;     len=strlen(str);     for(i=0;i<len;i++){     if(str[i]=='0')      ++n0;     else     ++n1;     }     if(n1==len-1 || n0==len-1)     printf("YES\n");     else     printf("NO\n");     } return 0; }

Arif likes to play volley ball. He found some statistics of matches which described who won the points in order.

#include<stdio.h> #include<string.h> #include<ctype.h> int main() {char matchscenario[102]; int t; scanf("%d",&t); while(t>0) {     int c=0,m=0,o=0;     scanf("%s",matchscenario);     for(int i=0;i<strlen(matchscenario);i++)     {       if(matchscenario[i]=='1')       m++;       else       o++;       if(m==11 || o==11)       { if(m==11)         c++;             break;}     }     if(c>0)     printf("WIN");     else     printf("LOSS");     printf("\n");     t--; } } 

Every day, Selvan goes to his office by train and buys the ticket from the counter on the day of travel. On the ticket, there is a letter-code that is represented as a string of upper-case Latin letters.Selvan believes that the day will be successful in case exactly two different letters in the code alternate.

 #include <stdio.h> int main() {   int n;     char ticketnumber[102];     scanf("%d",&n);     for (int i=0;i<n;i++)     {            scanf("%s",ticketnumber);         if(ticketnumber[0]!=ticketnumber[2]||ticketnumber [1]!=ticketnumber[3]||ticketnumber[3]!=ticketnumber [5])         {printf("NO\n");}         else         {printf("YES\n");}     } return 0; }