×

Day 1 Challenge

Posted on 18 April,2018 at 10pm IST


Now, we assume you learnt all the basics of programming in various programming languages like C , C++, Java. The question is do you really know what actual programming in real IT industry looks like ? What are the challenges programmers are facing while solving complex problems like Booking a ride on Uber where users can get the exact locations at real time. This is really cool I think because we the programmers have now much power in technology. So we decided to represent you some logic problems which gives you hands on experience in thinking about how to solve the problems .

Day 1 Coding Challenge. How to solve the problems of coding ?

Suppose you have given a word,

Example 1 :-

iAmTheMemberOfCodzify

In this challenge, we have to count the number of words in a given string such that each word is in Camel Caseing Format except the first word. In the given string we have 6 words in total which is the real output of this challenge.

1) i
2) Am
3) The
4) Member
5) Of
6) Codzify

Example 2 :-

weAllLoveProgramming
1) we
2) All
3) Love
4) Programming

There are total 4 words in the string of example 2. Three words are in Camel Caseing format which means first character is always in uppercase and rest ones in small letters.


How to solve this ?

We pick C programming to solve this program.

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


int camelcase(char* s) {
    // Complete this function
int count_words=0,i;
 char str[20];
 for(i=0; s[i] != '\0'; i++)
 {
   if (s[i] >= 'A' && s[i] <= 'Z')
      count_words++;
 }
 return count_words;
}


int main() {
    char* s = (char *)malloc(512000 * sizeof(char));
    scanf("%s", s);
    int result = camelcase(s);
    printf("%d",result+1);
    return 0;
}

So, program starts with main() function where it allocates memeory using malloc() function. To learn more about malloc() Refer here. Then the function camelcase(s) is called where actually the logic is written for counting the words in a given string.

Inside camelcase function,

for(i=0; s[i] != '\0'; i++)

This for loop runs till it finds the null charcater at the end of string. To learn more about '\o' (null character) you can Refer here..

 if (s[i] >= 'A' && s[i] <= 'Z')
      count_words++;

Suppose given string is myNameIsManish. Given if condition inside for loop checks each individual character wether it is in the list of capital A to Z letters. If it is, then increase the count_words else increment the loop counter and run it again.

In first loop when i = 0, it finds character m. It fails if condition because m is not in range of A to Z So loop counter increments by 1 and i will become equals to 1. Then it checks y which again fails if condition. Loop counter increments and i = 2. Now it checks N and it satisfies the if condition so count_words variable increases by 1. Again loop counter increments and as a fails if condition loop counter again increments and it checks m then e then I . As I satisfies if condition count_words increments by 1 . So count_words = 2 . In this way loop continues till it reaches Null character.

 Inside main() function,
    printf("%d",result+1);

result + 1 is because first word in string is always in lower case so to count it we have to increment the result inside camecase function by 1.