Techhgeekz ™

Saturday 31 August 2013

finding the length of the char array without '\0'

finding the length of the char array without '\0'

#include <stdio.h>
#include <string.h>
int main()
{
    char *s[]={"s","a","b"};
int a= sizeof(s) / sizeof(char *);  // 8(pointer char array) x 3(no.of char)=24 //24/8=3
printf("%d",a);
}

guessing a word with three times

guessing a word with three times in c using functions:-

#include <stdio.h>
#include <string.h>
void get_user_input(char *guess[10]);
int main(void)
{
const char secret[10] = "dinesh dk";
char guess[10];
int i;
for (i=0; i < 3; i++)
{
 get_user_input(guess);
 if (strcmp(secret, guess)==0)
 {
   printf("Your guess was correct");
   return 0;
 }
  else
 {
   printf("Your guess was incorrect. Please try again\n");
 }
 } 
return 0;
}
void get_user_input(char *guess[10])

 printf("Please guess the word: \n");
 scanf("%s", guess);
}

Friday 30 August 2013

program for converting numbers to words

simple c program for converting numbers to words:-

#include<stdio.h>
int main()
{
int number,i=0,j,digit;
char * word[1000];
printf("Enter any integer: ");
scanf("%d",&number);
while(number)
{
digit = number %10;
number = number /10;
switch(digit)
{
case 0: word[i++] = "zero"
break;
case 1: word[i++] = "one"
break;
case 2: word[i++] = "two"
break;
case 3: word[i++] = "three"
break;
case 4: word[i++] = "four"; 
break;
case 5: word[i++] = "five"
break;
case 6: word[i++] = "six"
break;
case 7: word[i++] = "seven"
break;
case 8: word[i++] = "eight"
break;
case 9: word[i++] = "nine"
break;
}
}
for(j=i-1;j>=0;j--)
{
printf("%s ",word[j]);
}
return 0;
}

Thursday 29 August 2013

common alphabet between two strings


FINDING COMMON ALPHABETS B/W TWO GIVEN STRINGS


#include<iostream>
using namespace std;
int main()
{
    int len1, len2, x;
    string s1="AJITH KUMAR", s2="PRAVEEN R";
    len1= s1.length();
    len2= s2.length();
    for(int i=0;i<len1;i++)
    {
    for(int j=0;j<i;j++)
    {
    if(s1[i]==s1[j])
    {
    x=0;
    break;
    }
    }
    if(x==0)
    {
    x= 1;
    continue;
    }
    for(int k=0;k<len2;k++)
    {
    if(s2[k]==s1[i])
    {
    cout<<s1[i]<<" ";
    break;
    }
    }
    }
    return 0
}