Write A Program To Recognize Identifiers: Code
Write A Program To Recognize Identifiers: Code
CODE:
#include<stdio.h>
#include<string.h>
int main()
{
char string[25];
int count=0,flag;
printf("enter any string: ");
gets(string);
if( (string[0]>='a'&&string[0]<='z') //small letter
||
||
(string[0]=='_') //underscore
{
for(int i=1;i<=strlen(string);i++)
if((string[i]>='a'&& string[i]<='z')
||
||
(string[i]>='0'&& string[i]<='9')
||
(string[i]=='-')
{
count++;
}
}
if(count==strlen(string))
{
flag=0;
}
else
{
flag=1;
}
if(flag==1)
printf("It is not valid identifier");
else
printf("It is valid identifier");
return 0;
}
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
char *s;
s = malloc(1024 * sizeof(char));
scanf("%[^\n]", s);
s = realloc(s, strlen(s) + 1);
for(int i = 0; i < strlen(s) ; i++)
{
if(s[i] == ' ')
printf("\n");
else
printf("%c",s[i]);
}
return 0;
}
//Try to run in online IDE and Give space after each token at the time of input
3. Write a C program that will replace a malloc() function to calloc() function with
appropriate parameters.(E.g.- Input: malloc(2*sizeof(int)), Output: calloc(2, sizeof(int)))
CODE:
#include <stdio.h>
#include <string.h>
#include <math.h>
/*
Input: malloc(25*sizeof(int))
Output: calloc(25,sizeof(int))
Input: malloc(9*sizeof(float))
Output: calloc(9,sizeof(float))
Input: malloc(256*sizeof(double))
Output: calloc(256,sizeof(double))
*/
int main(){
char string[500],type[20];
int size,i;
size = getSize(string);
i = 7 + floor(log10(size))+ 1 + 7 + 1; // Index of sizeof bracket
getType(string, type, i);
printf("calloc(%d,sizeof(%s))",size,type);
return 0;
}
type[idx] = '\0';
}
return size;
}
6. Write a C program to find the number of alphabets, digits and special characters in a
given input.
CODE:
#include <stdio.h>
#define MAX_SIZE 100 // Maximum string size
int main()
{
char str[MAX_SIZE];
int alphabets, digits, others, i;
/*
* Check each character of string for alphabet, digit or special character
*/
while(str[i]!='\0')
{
if((str[i]>='a' && str[i]<='z') || (str[i]>='A' && str[i]<='Z'))
{
alphabets++;
}
else if(str[i]>='0' && str[i]<='9')
{
digits++;
}
else
{
others++;
}
i++;
}
return 0;
}
7.Write a C program which will take a C file as input and find out the total memory space
required for allocating all the variables and check if it exceeds a certain limit.(which is
taken as user input)
CODE:
#include <stdio.h>
#include <string.h>
/*
Input: int a,b,c;float x,y;
Output: Total memory required 20 bytes
*/
int main(){
char string[1000];
char *line;
int totalSize = 0;
scanf("%[^\n]%*c", string);
do{
totalSize += parseLine(line);
line = strtok(NULL,";");
}while(line != NULL);
parseType(line,type);
varCount = countVariables(line);
while(*ch != '\0'){
if(*ch == ',')
count++;
ch+=1;
}
return count;
}
#include <stdio.h>
#include <string.h>
/*
Input: Filename test.txt
Output: void, int,
inside test.txt
void main()
{
int a = 10;
}
*/
int main(){
FILE *fptr;
char filename[100], ch, word[100];
int i=0;
11. Write a C program that should check if all the members of the structures are having a
defined data type. If not, print an error.
CODE:
#include <stdio.h>
#include <string.h>
int main(){
FILE *fptr;
char ch,filename[50],token[50];
int i=0,bracOpen=0,validTypes = 1;
char line[500];
fptr = fopen(filename,"r");
if(fptr == NULL){
printf("File not found");
return 0;
}
else{
validTypes = parseValidDataType(line);
if(checkForBracket(line,'}') || validTypes == 0) // closing bracket check
break;
}
}
if(validTypes == 1)
printf("The structure has defined datatypes\n");
else
printf("The structure has invalid datatypes\n");
fclose(fptr);
return 0;
}
type[i] = '\0';
if(checkForBracket(type,'}')==1)
return 1;
return 0;
}
return 0;
}
12. Write a program to ignore the comments in the given input source program(i.e., delete
them).
CODE:
#include <stdio.h>
#include <string.h>
/*
Input: Filename test.txt
Output:
void main()
{
int a=10;
printf("%d",a);
}
Inside test.txt
void main()
{
int a=10;
// Printing a variable;
printf("%d",a);
}
*/
int main(){
FILE *fptr;
char line[1000], filename[100], ch, program[10000];
int i=0;
program[0] = '\0';
fptr = fopen(filename,"r");
if(fptr == NULL){
printf("File not found");
return 0;
}
ch = fgetc(fptr);
13. Write a C program that will check whether the input string is containing “Monday” in
it.
CODE:
#include<stdio.h>
int main()
{
char str[80], search[10];
int count1 = 0, count2 = 0, i, j, flag;
printf("Enter a string:");
gets(str);
printf("Enter search substring:");
gets(search);
while (str[count1] != '\0')
count1++;
while (search[count2] != '\0')
count2++;
for (i = 0; i <= count1 - count2; i++)
{
for (j = i; j < i + count2; j++)
{
flag = 1;
if (str[j] != search[j - i])
{
flag = 0;
break;
}
}
if (flag == 1)
break;
}
if (flag == 1)
printf("SEARCH SUCCESSFUL!");
else
printf("SEARCH UNSUCCESSFUL!");
return 0;
}
14. Write a C program that will take a C file as an input and output a file which will have
\n and \b replaced by corresponding spaces.
CODE:
#include<stdio.h>
#include<string.h>
int main()
{
char str[100];
printf("Enter a string : ");
gets(str);
int l=strlen(str);
for(int i=0;i<l;i++)
{
if(str[i]==92)
{
if(str[i+1]=='t')
{
str[i]=' ';
str[i+1]=' ';
}
else
{
str[i]=' ';
str[i+1]='\n';
}
}
}
printf("%s",str);
return 0;
15. Write a C program that will check whether all the variables declared in an input file
are initialized or not. If not,initialize them with 0.
CODE:
#include<stdio.h>
#include<stdlib.h>
int main()
{
int a=2,b=0,c;
if( !c)
{
c=0;
printf("Value of the variable after initialization=%d",c);
}
else
{
printf("Value of the variable is=%d",c );
}
return 0;
}
#include<stdio.h>
int main()
{
char text[100];
int i=2,a=0;
printf("\n\nEnter Text : ");
gets(text);
if(text[0]=='/')
{
if(text[1]=='/')
{
printf("\nIt is a Comment.");
}
else if(text[1]=='*')
{
for(i=2;i<=100;i++)
{
if(text[i]=='*'&&text[i+1]=='/')
{
a=1;
break;
}
else continue;
}
if(a==0)
{
printf("\nIt is Not a Comment.");
}
else
{
printf("\nIt is a Comment.");
}
}
else
{
printf("\nIt is Not a Comment.");
}
return 0;
}
}
#include <stdio.h>
#define max 40
#include <string.h>
return 0;
}
20. Write a C program to replace all the digits in a file to their corresponding words. Use a
switch case.
CODE:
#include <stdio.h>
#include <math.h>
int main()
{
int n, num = 0, digits;
while(n != 0)
{
num = (num * 10) + (n % 10);
n /= 10;
}
/*
* Extract last digit of number and print corresponding number in words
* till num becomes 0
*/
while(num != 0)
{
switch(num % 10)
{
case 0:
printf("Zero ");
break;
case 1:
printf("One ");
break;
case 2:
printf("Two ");
break;
case 3:
printf("Three ");
break;
case 4:
printf("Four ");
break;
case 5:
printf("Five ");
break;
case 6:
printf("Six ");
break;
case 7:
printf("Seven ");
break;
case 8:
printf("Eight ");
break;
case 9:
printf("Nine ");
break;
}
num /= 10;
}
while(digits)
{
printf("Zero ");
digits--;
}
return 0;
}
21. Write a program in C that will take two files as input and merge them into one and
delete any redundant words from the resulting file.
CODE:
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *fs1, *fs2, *ft;
printf("Enter name of file which will store contents of the two files\n");
gets(file3);
if (ft == NULL)
{
perror("Error ");
printf("Press any key to exit...\n");
exit(EXIT_FAILURE);
}
fclose(fs1);
fclose(fs2);
fclose(ft);
return 0;
}
#include <stdio.h>
int all_upper(char ch[5]){
int i= 0, flag=1;
for(;ch[i]!='\0'; i++)
if(ch[i]>'Z')
flag = 0;
return flag;
}
int main(){
char left[5], right[5];
printf("Enter the left production: ");
scanf("%s", left);
fflush(stdin);
printf("\nEnter the right production: ");
scanf("%s", right);
//if(all_upper(left) && all_lower(right))
if(all_upper(left))
printf("CFG");
else
printf("Not");
23. Write a C program to count the number of white spaces between two consecutive
tokens in a program and replace it with a single whitespace.
CODE:
#include <ctype.h>
#include <stdio.h>
24. Write a program in C to find the First and Follow for a given set of productions
CODE:
#include<stdio.h>
#include<string.h>
#include<conio.h>
int main()
{
char t[5],nt[10],p[5][5],first[5][5],temp;
int i,j;
int z=0;
int nont=0;
int k=0,f=0;
printf("\nEnter the no. of Non-terminals in the grammer:");
scanf("%d",&nont);
printf("\nEnter the Non-terminals in the grammer:\n");
for(i=0;i<nont;i++)
{
scanf("\n%c",&nt[i]);
}
printf("\nEnter the no. of Terminals in the grammer: ( Enter { for absiline )");
scanf("%d",&z);
printf("\nEnter the Terminals in the grammer:\n");
for(i=0;i<z||t[i]=='$';i++)
{
scanf("\n%c",&t[i]);
}
for(i=0;i<nont;i++)
{
p[i][0]=nt[i];
first[i][0]=nt[i];
}
printf("\nEnter the productions :\n");
for(i=0;i<nont;i++){
scanf("%c",&temp);
printf("\nEnter the production for %c ( End the production with '$'sign ) :",p[i][0]);
for(j=0;p[i][j]!='$';)
{
j+=1;
scanf("%c",&p[i][j]);
}
}
for(i=0;i<nont;i++)
{
printf("\nThe production for %c -> ",p[i][0]);
for(j=1;p[i][j]!='$';j++)
{
printf("%c",p[i][j]);
}
}
for(i=0;i<nont;i++)
{
f=0;
for(j=1;p[i][j]!='$';j++)
{
for(k=0;k<z;k++)
{
if(f==1)
break;
if(p[i][j]==t[k])
{
first[i][j]=t[k];
first[i][j+1]='$';
f=1;
break;
}
else if(p[i][j]==nt[k]){
first[i][j]=first[k][j];
if(first[i][j]=='{')
continue;
first[i][j+1]='$';
f=1;
break;
}
}
}
}
for(i=0;i<nont;i++)
{
printf("\n\nThe first of %c -> ",first[i][0]);
for(j=1;first[i][j]!='$';j++)
{
printf("%c\t",first[i][j]);
}
}
return 0;
}
#include<stdio.h>
#include<string.h>
int main()
{
char str[20];
printf("Enter left side of production : ");
gets(str);
char str2[20];
printf("Enter right side of production : ");
gets(str2);
int l1=strlen(str);
int l2=strlen(str2);
if((str[0]==str2[0])&&(str[l1-1]==str2[l2-1]))
printf("The grammar is context-sensitive");
else
printf("The grammar is not context-sensitive");
return 0;
}
#include<stdio.h>
#include<string.h>
int main()
{
char str[100];
printf("Enter string : ");
gets(str);
int flag=0;
int l=strlen(str);
if((str[0]=='h'))
flag++;
if((str[1]=='t'))
flag++;
if((str[2]=='t'))
flag++;
if((str[3]=='p'))
flag++;
if((str[4]=='s'))
flag++;
if((str[5]==':'))
flag++;
if((str[6]=='/')&&(str[7]=='/'))
flag++;
if((str[8]=='w')&&(str[9]=='w')&&(str[10]=='w')&&(str[11]=='.'))
flag++;
if((str[l-1]=='m')||(str[l-1]=='n')||(str[l-1]=='g'))
flag++;
if(flag>=9)
printf("\nValid web address.");
if(flag<=8)
printf("\nNot a valid web address.");
return 0;
}
29. Write a C program to convert all the lowercase alphabets in a file to uppercase and
vice- versa.
CODE:
#include<stdio.h>
void main() {
FILE *fp1, *fp2;
char a;
do {
a = fgetc(fp1);
if(a>='a' && a<='z')
a = toupper(a);
else if(a>='A' && a<='Z')
a = tolower(a);
else
a = a;
fputc(a, fp2);
} while (a != EOF);
fcloseall();
getch();
}
#include<stdio.h>
char data[10];
int flag=0;
int is_upper_alphabet(char l)
{
if(l>='A' && l<='Z')
{
return 1;
}
return 0;
}
int is_small(char l)
{
if(l>='a' && l<='z')
{
return 1;
}
return 0;
}
int check(int i)
{
while(data[i]!=';')
{
31. Write a C program which can recognize whether an email address is valid or not.
CODE:
#include<stdio.h>
#include<string.h>
int main()
{
char str[100];
int flag=0;
printf("Enter a email: \n");
gets(str);
int l=strlen(str);
for(int i=0;i<l;i++)
{
if(str[i]=='@')
flag=1;
}
if(flag==1)
printf("Email is valid");
else
printf("Email is not valid");
return 0;
}
32. Given a set of alphabets {a,b,c}, write a C program to find all the possible strings of
length at most 3.
CODE:
#include <stdio.h>
#include <string.h>
int getWords(char *base, char target[10][20])
{
int n=0,i,j=0;
for(i=0;1;i++)
{
if(base[i]!=' '){
target[n][j++]=base[i];
}
else{
target[n][j++]='\0';//insert NULL
n++;
j=0;
}
if(base[i]=='\0')
break;
}
return n;
}
int main()
{
//read string
printf("Enter a string: ");
scanf("%[^\n]s",text); //to read string with spaces
char arr[10][20];
n=getWords(text,arr);
for(i=0;i<=n;i++)
if(strlen(arr[i])<=3){
printf("%s ",arr[i]);
}
return 0;
}
34. Write a C program to find the number of new lines in a program
CODE:
#include<stdio.h>
#include<string.h>
int main()
{
char str[100];
int count=1;
int count2=0;
printf("Enter string : ");
gets(str);
int l=strlen(str);
for(int i=0;i<l;i++)
{
if((str[i]==92))
count++;
if(str[i]==' ')
count2++;
}
printf("Blank Space = %d",count2);
printf("\nNew line = %d",count);
return 0;
}
35. Write a C program to print the number of times a certain function is called.
CODE:
#include<stdio.h>
static int c;
static int d;
void fun()
{
c++;
}
void fun2()
{
d++;
}
int main()
{
c=0;
d=0;
fun();
fun();
fun();
fun();
fun2();
fun2();
fun2();
if(c==1)
printf("Function fun is called %d time\n",c);
else
printf("Function fun is called %d times\n",c);
if(d==1)
printf("Function fun2 is called %d time",d);
else
printf("Function fun2 is called %d times",d);
return 0;
}
36. Write a C program which will copy each line of a given program and number each
newline.
CODE:
#include <stdio.h>
int main()
{
FILE *fileptr;
int count_lines = 1, i = 0;
char filechar[40], chr, line[100];
37. Write a program in C to print an error when a user doesn’t provide a semi-colon at
the end of a line of a program. User input should be a file containing a program.
CODE:
#include <stdio.h>
int main()
{
FILE *fileptr;
int count_lines = 0, counter_semicolon = 0;
char filechar[40], chr;
#include <stdio.h>
#include <string.h>
int main()
{
char a[100], b[100];
strcat(a,b);
return 0;
}
39. Write a program in C to count the number of blank spaces and print the number of
lines.
CODE:
#include<stdio.h>
#include<string.h>
int main()
{
char str[100];
int count=1;
int count2=0;
printf("Enter string : ");
gets(str);
int l=strlen(str);
for(int i=0;i<l;i++)
{
if((str[i]==92))
count++;
if(str[i]==' ')
count2++;
}
printf("Blank Space = %d",count2);
printf("\nNew line = %d",count);
return 0;
}
41. Write a C program that will count the number of lowercase and uppercase characters
from a file.
CODE:
#include <stdio.h>
int main()
{
char str[100];
int countL,countU;
int counter;
for(counter=0;str[counter]!=NULL;counter++){
return 0;
}