Search results for char
Example:[1]
Write a C Program macro to check if a character is alphabetic or not.
#include <stdio.h>
#define ALPHA(CH) ((CH >= 'a' && CH <= 'z') || (CH >= 'A' && CH <= 'Z')?1:0)
int alp(int );
int main()
{
int al;
char a = 'a';
al = alp(a);
if(al == 1)
{
printf("the given char is alpha");
}else
{
printf("the given char is not alpha");
}
return 0;
}
int alp(int a)
{
return ALPHA(a);
}
Example:[2]
Write a c program function to create simple calculator using switch case.
#include <stdio.h>
int main()
{
char op;
float num1, num2, result=0.0f;
printf("Enter [number 1] [operator] [number 2]\n");
scanf("%f %c %f", &num1, &op, &num2);
switch(op)
{
case '+':
result = num1 + num2;
break;
case '-':
result = num1 - num2;
break;
case '*':
result = num1 * num2;
break;
case '/':
result = num1 / num2;
break;
default:
printf("Unsupported Operator");
}
printf("%.2f %c %.2f = %.2f\n", num1, op, num2, result);
return 0;
}
Example:[3]
Write a C program function to take a character array in array format and return the last character of the array passed to it.
#include <stdio.h>
#define SIZE 5
char char_count(char a[]);
int main()
{
char arr[] = {'H','e','l','l','o'};
char last;
last = char_count(arr);
printf("The last character of the array passed is %c\n",last);
return 0;
}
char char_count(char array[])
{
return array[SIZE-1];
}
Example:[4]
Write a C program to count the number of characters in a string without using the c inbuilt library.
#include <stdio.h>
int NoOfChars(char p[]);
int main()
{
int no_chr;
char str[] = "Hello How are you";
no_chr = NoOfChars(str);
printf("No of chars is: %d\n",no_chr);
return 0;
}
int NoOfChars(char d[])
{
int i,count = 0;
for(i = 0;d[i] != '\0';i++)
{
count++;
}
return count;
}
Example:[5]
Write a C program function to reverse the string passed to it without using the c inbuilt library.
#include <stdio.h>
void RevString(char p[],char r[]);
void main()
{
char str[] = "Hello";
char res[10];
RevString(str,res);
printf("Reversed string is: %s\n",res);
}
void RevString(char d[],char s[])
{
int i,j,k,len = 0;
for(i = 0;d[i] != '\0';i++)
{
len++;
}
for(j = 0,k = len-1;d[j] != '\0';j++,k--)
{
s[j] = d[k];
}
s[len] = '\0';
}
Example:[6]
Write a c program to accept a string as input and return the last character of the string as output of the function.
#include <stdio.h>
char RetLastChar(char *str);
int main()
{
char *in = "Hello";
char res;
res = RetLastChar(in);
printf("The last char is %c\n",res);
return 0;
}
char RetLastChar(char *str)
{
int i;
char out;
for(i = 0;str[i] != '\0';i++)
{
out = str[i];
}
return out;
}
Example:[7]
Write a c program function that reverses the string passed to it without declaring any new string within the function.
#include <stdio.h>
void reverse(char a[]);
int main() {
char str[10] = "Hello";
reverse(str);
printf("\nReverse string is :%s\n", str);
return (0);
}
void reverse(char d[])
{
int k,len = 0;
char temp;
int i = 0;
for(k = 0;d[k] != '\0';k++)
{
len++;
}
while(i < (len - 1))
{
temp = d[i];
d[i] = d[len-1];
d[len-1] = temp;
i++;
len--;
}
}
Example:[8]
Write a program to find the number of times that a given word(i.e. a short string) occurs in a sentence (i.e. a long string!).
#include <stdio.h>
#include <string.h>
unsigned int search_word(char *,char *);
int main()
{
char *s_str = "the";
char *l_str = "the word searching for from these sentence is the.";
unsigned int result;
result = search_word(s_str,l_str);
printf("Small string is found %d times in long string\n",result);
return 0;
}
unsigned int search_word(char *s1,char *s2)
{
int times = 0;
int i = 0,j = 0,k = 0;
int size = strlen(s1);
printf("Size of small string \" %s \" is: %d\n",s1,size);
while(s2[j] != '\0')
{
if(j >= (size - 1))
{
if(s2[j] == s1[size-1] && s2[j-1] == s1[size-2] && s2[j-2] == s1[size-3])
{
times = times + 1;
}
}
j++;
}
return times;
}
Example:[9]
Write a c program function to accept a string and return the character written two times continuously in the string.
#include <stdio.h>
char res_char(char *);
int main()
{
char *str = "Hello how are you";
char result;
result = res_char(str);
printf("Got result %c\n",result);
return 0;
}
char res_char(char *f)
{
char res;
while(*f != '\0')
{
if(*f == *(f + 1))
{
res = *f;
}
f++;
}
return res;
}
Example:[10]
Write a C program function to count frequency of each character in a string and return the maximum frequency.
#include <stdio.h>
int freq_func(char *);
int main()
{
char *s = "Here is the string that needs to be verified for the frequency of each character";
int max_freq;
max_freq = freq_func(s);
printf("Max Frequency is:%d\n",max_freq);
return 0;
}
int freq_func(char *r)
{
int i =0,j = 0,res1 = 0,res2 = 0;
while(r[j] != '\0')
{
while(r[i] != '\0')
{
if(r[j] == r[i])
{
res1 = res1 + 1;
}
i++;
}
if(res1 > res2)
{
res2 = res1;
}
i = 0;
res1 = 0;
j++;
}
return res2;
}
Example:[11]
Write a C program function to take a string as its input and return the first word of the string passed to it.
#include <stdio.h>
void getfword(char *s, char f_word[]);
int main() {
char str[] = "Hello how are you";
char res[20];
getfword(str,res);
printf("Result is:%s\n", res);
}
void getfword(char *s, char f_word[])
{
int i = 0;
while(s[i] != '\0')
{
if(s[i] == ' ')
{
break;
}else
{
f_word[i] = s[i];
f_word[i + 1] = '\0';
i++;
}
}
}
Example:[12]
Write a c program function to calculate the number of words present in a string passed to it.
#include <stdio.h>
int NumOfWords(char *s);
int res = 1;
int main() {
char str[] = "Hello how are you";
res = NumOfWords(str);
printf("Result is:%d\n", res);
}
int NumOfWords(char *s)
{
int i = 0;
while(s[i] != '\0')
{
if(s[i] == ' ')
{
res = res + 1;
}
i++;
}
return res;
}
Example:[13]
Write a c program function to reverse the string passed to it using pointers and without using the c inbuilt library.
#include <stdio.h>
#include <stdlib.h>
void RevString(char *,char *);
int main()
{
char *str = "Hello";
char *res;
res = (char *)malloc(10);
RevString(str,res);
printf("Reversed string is %s\n",res);
return 0;
}
void RevString(char *s,char *r)
{
int i,j,k,len = 0;
for(i = 0;*(s+i) != '\0';i++)
{
len++;
}
for(j = 0,k = len-1;*(s+j) != '\0',k > -1;j++,k--)
{
*(r+j) = *(s+k);
}
}
Example:[14]
Write a c program function to return the same string after reversing it using pointers
#include <stdio.h>
#include <string.h>
void reverseString(char* str)
{
int l, i;
char *begin_ptr, *end_ptr, ch;
l = strlen(str);
begin_ptr = str;
end_ptr = str;
for (i = 0; i < l - 1; i++)
{
end_ptr++;
}
for (i = 0; i < l / 2; i++)
{
ch = *end_ptr;
*end_ptr = *begin_ptr;
*begin_ptr = ch;
begin_ptr++;
end_ptr--;
}
}
int main()
{
char str[10] = "Hello";
reverseString(str);
printf("Reverse of the string: %s\n", str);
return 0;
}
Example:[15]
Write a c program function to take your name as input using fgets stdin and print the name as output
#include <stdio.h>
#include <stdlib.h>
#define MAX_COUNT 20
void getName(char *name);
void main(){
char myName[20];
getName(myName);
printf("Your name:%s\n",myName);
}
void getName(char *name){
printf("Enter your name:");
fgets(name,MAX_COUNT,stdin);
}
Example:[16]
Write a c program to accept a string as input and return the number of words present in it using pointers.
#include <stdio.h>
void NumOfWords(char *s, int *);
int main() {
char str[] = "Hello how are you";
int res = 1;
NumOfWords(str,&res);
printf("Result is:%d\n", res);
}
void NumOfWords(char *s, int *r)
{
int i = 0;
while(s[i] != '\0')
{
if(s[i] == ' ')
{
*r = *r + 1;
}
i++;
}
}
Example:[17]
Write a function to loop through the strings present in an array of pointers to string
#include <stdio.h>
char *StringSet[] = {
"string1 1 0 2 0",
"string2 1 2 0 5 9",
"string3 0 9 2",
""
};
int main(){
int i = 0;
while(*StringSet[i] != '\0'){
printf("String %s\n",StringSet[i]);
i++;
}
}
Example:[18]
Write a program to swap the 4bytes of an integer. If input is provided as 0x12345678 output is generated as 0x78563412.
#include <stdio.h>
union out
{
struct
{
unsigned char ll;
unsigned char lh;
unsigned char hl;
unsigned char hh;
}in;
unsigned int b;
};
int main()
{
union out aa;
aa.b = 0x12345678;
printf("Before swapping:%x\n", aa.b);
unsigned char ll = aa.in.ll;
unsigned char lh = aa.in.lh;
aa.in.ll = aa.in.hh;
aa.in.hh = ll;
aa.in.lh = aa.in.hl;
aa.in.hl = lh;
printf("After swapping:%x\n", aa.b);
return 0;
}
Example:[19]
Write a function to determine whether the machine is Little Endian or Big Endian.
#include <stdio.h>
unsigned int EndianCheck();
union endian
{
unsigned int a;
char b;
};
int main()
{
unsigned int res;
res = EndianCheck();
if(res == 0)
{
printf("Little endian\n");
}else
{
printf("Big endian\n");
}
return 0;
}
unsigned int EndianCheck()
{
union endian test;
test.a = 1;
if(test.b == 1)
{
return 0;
}else
{
return 1;
}
}
Example:[20]
Write a C program function to enter different book information(pages, author, search id) in a school library.
#include <stdio.h>
#define NO_BOOKS 3
struct Book
{
int pages;
char author[10];
int search_id;
};
void EnterBooks(struct Book *);
int main()
{
struct Book b[NO_BOOKS];
EnterBooks(b);
return 0;
}
void EnterBooks(struct Book *a)
{
int i;
for(i = 0;i < NO_BOOKS;i++)
{
printf("Enter %d book pages\n", i+1);
scanf("%d",&a->pages);
printf("Enter %d book author\n", i+1);
scanf("%s",&a->author);
printf("Enter %d search id\n", i+1);
scanf("%d",&a->search_id);
a++;
}
}
Example:[21]
Write a C program function to enter different book information(pages, author, search id) in a school library and display the existing books using structure pointers.
#include <stdio.h>
#include <stdlib.h>
#define NO_BOOKS 3
struct Book
{
int pages;
char author[10];
int search_id;
};
void EnterBooks(struct Book *);
void DisBooks(struct Book *);
int main()
{
struct Book *b;
b = (struct Book *)malloc(sizeof(struct Book)*NO_BOOKS);
if(b == NULL)
{
printf("Req memory allocation failed\n");
}else
{
EnterBooks(b);
DisBooks(b);
}
free(b);
return 0;
}
void EnterBooks(struct Book *a)
{
int i;
for(i = 0;i < NO_BOOKS;i++)
{
printf("Enter %d book pages\n", i+1);
scanf("%d",&a->pages);
printf("Enter %d book author\n", i+1);
scanf("%s",&a->author);
printf("Enter %d search id\n", i+1);
scanf("%d",&a->search_id);
a++;
}
}
void DisBooks(struct Book *a)
{
int i;
for(i = 0;i < NO_BOOKS;i++)
{
printf("Book %d pages %d\n",i+1,a->pages);
printf("Book %d author %s\n",i+1,a->author);
printf("Book %d book search id %d\n",i+1,a->search_id);
a++;
}
}
Example:[22]
Write a C program function to enter different book information(pages, author, search id) in a school library and display the existing books using structure arrays.
#include <stdio.h>
#define NO_BOOKS 3
struct Book
{
int pages;
char author[10];
int search_id;
};
void EnterBooks(struct Book *);
void DisplayBooks(struct Book *);
int main()
{
struct Book b[NO_BOOKS];
EnterBooks(b);
DisplayBooks(b);
return 0;
}
void EnterBooks(struct Book *a)
{
int i;
for(i = 0;i < NO_BOOKS;i++)
{
printf("Enter %d book pages\n", i+1);
scanf("%d",&a->pages);
printf("Enter %d book author\n", i+1);
scanf("%s",&a->author);
printf("Enter %d search id\n", i+1);
scanf("%d",&a->search_id);
a++;
}
}
void DisplayBooks(struct Book a[])
{
int i;
for(i = 0;i < NO_BOOKS;i++)
{
printf("Book %d pages %d\n",i+1,a[i].pages);
printf("Book %d author %s\n",i+1,a[i].author);
printf("Book %d book search id %d\n",i+1,a[i].search_id);
}
}
Example:[23]
Write a C program function to enter different book information(pages, author, search id) in a school library and search any book using search id and structure arrays.
#include <stdio.h>
#define NO_BOOKS 3
struct Book
{
int pages;
char author[10];
int search_id;
};
void EnterBooks(struct Book *);
struct Book *SrchBook(struct Book a[]);
int main()
{
struct Book b[NO_BOOKS];
EnterBooks(b);
struct Book *s;
s = SrchBook(b);
printf("Book pages:%d\n", s->pages);
printf("Book author:%s\n", s->author);
printf("Book search_id:%d\n", s->search_id);
return 0;
}
void EnterBooks(struct Book *a)
{
int i;
for(i = 0;i < NO_BOOKS;i++)
{
printf("Enter %d book pages\n", i+1);
scanf("%d",&a->pages);
printf("Enter %d book author\n", i+1);
scanf("%s",&a->author);
printf("Enter %d search id\n", i+1);
scanf("%d",&a->search_id);
a++;
}
}
struct Book *SrchBook(struct Book a[])
{
int i,id;
printf("Enter the book search id\n");
scanf("%d",&id);
for(i = 0;i < NO_BOOKS;i++)
{
if(id == a[i].search_id)
{
return &a[i];
}
}
if(i == NO_BOOKS)
{
printf("Invalid Search_id, try again\n");
return NULL;
}
}
Example:[24]
Write a C program function to enter different book information(pages, author, search id) in a school library and search any book using search id and structure pointers.
#include <stdio.h>
#include <stdlib.h>
#define NO_BOOKS 3
struct Book
{
int pages;
char author[10];
int search_id;
};
void EnterBooks(struct Book *);
struct Book *SrchBook(struct Book *);
int main()
{
struct Book *b;
b = (struct Book *)malloc(sizeof(struct Book)*NO_BOOKS);
if(b == NULL)
{
printf("Req memory allocation failed\n");
}else
{
EnterBooks(b);
struct Book *s;
s = SrchBook(b);
printf("Book pages:%d\n", s->pages);
printf("Book author:%s\n", s->author);
printf("Book search_id:%d\n", s->search_id);
}
free(b);
return 0;
}
void EnterBooks(struct Book *a)
{
int i;
for(i = 0;i < NO_BOOKS;i++)
{
printf("Enter %d book pages\n", i+1);
scanf("%d",&a->pages);
printf("Enter %d book author\n", i+1);
scanf("%s",&a->author);
printf("Enter %d search id\n", i+1);
scanf("%d",&a->search_id);
a++;
}
}
struct Book *SrchBook(struct Book *a)
{
int i,id;
printf("Enter the book search id\n");
scanf("%d",&id);
for(i = 0;i < NO_BOOKS;i++)
{
if(id == a->search_id)
{
return &a[i];
}
a++;
}
if(i == NO_BOOKS)
{
printf("Invalid Search_id, try again\n");
return NULL;
}
}
Example:[25]
Write a C program function to swap the nibbles in a byte.
#include <stdio.h>
unsigned char function(unsigned char );
int main()
{
unsigned char pass = 0x12;
printf("Input byte is: %x\n", pass);
unsigned char res;
res = function(pass);
printf("The result is %x\n",res);
}
unsigned char function(unsigned char d)
{
return ((d << 4) | (d >> 4));
}