AD1

Tuesday, 28 February 2017

compute occurrence of character in a string

write C Program print the frequency of a certain character in a string 

#include <stdio.h>
#include <string.h>
int main(void) {
printf("enter the string\n");
char string[50];char c;int i=0;int counter=0;
gets(string);
printf("enter the character you want to compute its occurrence\n");
scanf("%c",&c);
while(string[i]!='\0')
{
if(string[i]==c){counter++;i++;}
else {i++;continue;}
}
printf("%c occured %d times with frequency %f %c\n",c,counter,100*(float)counter/i,'%');
return 0;
}
view raw freq.c hosted with ❤ by GitHub

what is the output of this program


#include <stdio.h>
void f(int (*x)(int));
int myfoo(int i);
int (*foo)(int) = myfoo;
int main()
{
f(foo(10));
}
void f(int (*i)(int))
{
i(11);
}
int myfoo(int i)
{
printf("%d\n", i);
return i;
}
view raw ptof.c hosted with ❤ by GitHub
output
10 segmentation fault

explanation


Wednesday, 22 February 2017

find the output

what will be the output of the following code 


#include <stdio.h>
int main(void) {
int x=100;char y=3;float z=3.12;
int *xp=&x;char *yp=&y;float *zp=&z;
printf("%d\t%d\t%d\t%d\t%d\n",sizeof(NULL),sizeof(xp),sizeof(yp),sizeof(zp),sizeof("clu"));
return 0;
}
view raw szp.c hosted with ❤ by GitHub
output will be 
8  8  8  4
size of a pointer to any data type is type independent , size of pointer depends on your machine address bus.
sizeof("clu")  here "clu" is treated as array of characters ={'c','l','u','\0'}
so sizeof("clu") is 4 bytes

Tuesday, 14 February 2017

palindrome number

Write A program to check if a positive integer is palindrome or not

#include <stdio.h>
#include <math.h>
int main()
{
int n;scanf("%d",&n);
int digits=0;int reverse=0;
int temp1=0;
int temp2=0;
if(n<=0){printf("enter positive enteger\n");}
else
{
temp1=n;
temp2=n;
while(temp1!=0)
{
temp1/=10;
digits++;
}
for(int i=digits-1;i>=0;i--)
{
reverse+=(temp2%10)*(pow(10,i));
temp2/=10;
}
if(reverse==n){printf("palindrome\n");}
else{printf("not palindrom\n");}
}
return 0;
}
view raw palindrom.c hosted with ❤ by GitHub

Saturday, 11 February 2017

Number of words in a string

Write a C function words(string) that accepts a string as input parameter and returns the number of words in this string 
as example
words("my name is mohamed") will return 4


#include <stdio.h>
#include <string.h>
int words(char *s)
{
int i=0;
int counter=0;
if(strlen(s)==0){return 0;}
else{
while(*(s+i)!='\0')
{
if(*(s+i)==' '){counter++;i++;}
i++;
}
return counter+1;
}
}
int main(int argc, char **argv)
{
char statement[]="";
printf("number of words are\n%d\n",words(statement));
return 0;
}
view raw no_of_words.c hosted with ❤ by GitHub

Friday, 10 February 2017

What will be the output of the following program

#include <stdio.h>
int main(int argc, char **argv)
{
char s[]="Egypt";
for(int i=0;*("Egypt"+i)!='\0';i++)
{
printf("%c",s[i]);
}
return 0;
}
view raw string.c hosted with ❤ by GitHub
the output is 
Egypt 
explanation 
string name is the base address of the string variable which is an array of characters

Sunday, 5 February 2017

Even or Odd

Write a C program to check if any entered number is even or odd without the use of conditional statements (if,else)

#include <stdio.h>
int main(void) {
printf("enter your number (assume it must be positive integer)\n");
int input;
scanf("%d",&input);
char choise[2]={'E','O'};// O for Odd and E for Even
printf("%d is %c",input,choise[input%2]);
return 0;
}
view raw evenorodd.c hosted with ❤ by GitHub

No loops

Write a C program to print the numbers from 1 to 10 without using any looping statements (for/while/do while)
also do not write printf() 10 times


#include <stdio.h>
int main()
{
static unsigned int counter=1;
if(counter<=10)
{
printf("%d\t",counter);
counter++;
main();
}
return 0;
}
view raw noloop.c hosted with ❤ by GitHub

Thursday, 2 February 2017

convert to uppercase

Write a function upper() that accepts a string and converts all its characters into uppercase characters 
as example upper("c programming")=C PROGRAMMING
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
bool is_upper(char x)
{
if((int)x>=65&&(int)x<=90)
{
return true;
}
return false;
}
char *upper(char *base)//passing array to a function that returns a pointer to char
{
while(*base!='\0')
{
if(is_upper(*base)||*base==' '){*base++;}
else{*base-=32;base++;}
}
return base;
}
int main()
{
char array[20]="hE l lO";
upper(array);
puts(array);
return 0;
}
view raw upper.c hosted with ❤ by GitHub


reverse a string

Write a function reverse() that accepts a string and reverses it 
as example reverse("hello") would return "olleh"
#include <stdio.h>
#include <string.h>
char* reverse(char *base)// passing array to a function that returns pointer to character
{
int i=0;
int j=strlen(base)-1;
char temp;
while(i!=j)
{
temp=base[i];
base[i]=base[j];
base[j]=temp;
i++;
j--;
}
return base;
}
int main()
{
char array[5]="hello";
puts(reverse(array));
return 0;
}
view raw reverse.c hosted with ❤ by GitHub

string of numbers

write a function that accepts string of numbers and converts it to digits 
as example convert("123")=123

#include <stdio.h>
void convert(char *base)// passing array to a function
{
while(*base!='\0')
{
*base-='0';//*base=*base-'0' will give you the numeric value of character number
printf("%d",*base);
base++;
}
}
int main()
{
char num_char[5]="83461";
convert(num_char);
return 0;
}
view raw convert.c hosted with ❤ by GitHub


Remove all blank spaces in a string

Write a C program to remove all the blank spaces from a string.
as example if string is "hello world , C programming" then the output will be "helloworld,Cprogramming"

#include <stdio.h>
#include <string.h>
int main()
{
char input_string[100];char output_string[100];// two char arrays (two strings)
printf("enter string with spaces\n");
gets(input_string);// you can not use scanf() as it do not accept string with spaces
int i=0;int j=0;
while(input_string[i]!='\0')
{
if(input_string[i]!=' ')
{
output_string[j]=input_string[i];
i++;
j++;
}
else
{
i++;
}
}
output_string[j]='\0';//terminate o/p string with null character
printf("input string without spaces \n");
puts(output_string);
return 0;
}
view raw remove.c hosted with ❤ by GitHub