KEMBAR78
Atcd Lab1 | PDF | Computer Programming | Software Engineering
0% found this document useful (0 votes)
3 views6 pages

Atcd Lab1

The document contains a C program that scans a given input string for various elements such as keywords, digits, operators, strings, character literals, and punctuation marks. Each type of element is processed by a specific function that identifies and prints them. The program uses standard input to read a string and then calls these functions to display the categorized elements.

Uploaded by

ayushvre1shete
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views6 pages

Atcd Lab1

The document contains a C program that scans a given input string for various elements such as keywords, digits, operators, strings, character literals, and punctuation marks. Each type of element is processed by a specific function that identifies and prints them. The program uses standard input to read a string and then calls these functions to display the categorized elements.

Uploaded by

ayushvre1shete
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

#include <stdio.

h>

#include <ctype.h>

#include <string.h>

#include <stdbool.h>

void scanChar(const char *text) {

printf("\nCharacter literals:\n");

bool state = false;

for (int pos = 0; text[pos] != '\0'; pos++) {

if (text[pos] == '\'') {

if (!state) {

state = true;

} else {

printf("'%c'\n", text[pos-1]);

state = false;

void scanPunct(const char *text) {

printf("\nPunctuation marks:\n");

for (int pos = 0; text[pos] != '\0'; pos++) {

if (ispunct(text[pos]) && text[pos] != '"' && text[pos] != '\'') {

switch (text[pos]) {

case '{': printf("{\n"); break;

case '}': printf("}\n"); break;

case '[': printf("[\n"); break;

case ']': printf("]\n"); break;

case '(': printf("(\n"); break;


case ')': printf(")\n"); break;

case ';': printf(";\n"); break;

case ':': printf(":\n"); break;

case ',': printf(",\n"); break;

case '.': printf(".\n"); break;

case '#': printf("#\n"); break;

case '_': printf("_\n"); break;

default: printf("%c\n", text[pos]); break;

void scanNum(const char *text) {

printf("\nDigits:\n");

for (int pos = 0; text[pos] != '\0'; pos++) {

if (isdigit(text[pos])) {

printf("%c\n", text[pos]);

void scanOp(const char *text) {

printf("\nOperators:\n");

for (int pos = 0; text[pos] != '\0'; pos++) {

switch (text[pos]) {

case '+': printf("+\n"); break;

case '-': printf("-\n"); break;

case '*': printf("*\n"); break;

case '/': printf("/\n"); break;

case '%': printf("%%\n"); break;


case '=': printf("=\n"); break;

case '<': printf("<\n"); break;

case '>': printf(">\n"); break;

case '&': printf("&\n"); break;

case '|': printf("|\n"); break;

case '!': printf("!\n"); break;

case '^': printf("^\n"); break;

void scanStr(const char *text) {

printf("\nStrings:\n");

bool state = false;

int mark = 0;

for (int pos = 0; text[pos] != '\0'; pos++) {

if (text[pos] == '"') {

if (!state) {

mark = pos;

state = true;

} else {

printf("\"");

for (int i = mark + 1; i < pos; i++) {

printf("%c", text[i]);

printf("\"\n");

state = false;

}
}

void scanKey(const char *text) {

const char *list[] = {

"auto", "break", "case", "char", "const", "continue", "default", "do", "double",

"else", "enum", "extern", "float", "for", "goto", "if", "int", "long", "register",

"return", "short", "signed", "sizeof", "static", "struct", "switch", "typedef",

"union", "unsigned", "void", "volatile", "while"

};

const int max = sizeof(list) / sizeof(list[0]);

printf("\nKeywords:\n");

char temp[50];

int count = 0;

for (int pos = 0; text[pos] != '\0'; pos++) {

if (isalpha(text[pos]) || text[pos] == '_') {

temp[count++] = text[pos];

} else if (count > 0) {

temp[count] = '\0';

for (int i = 0; i < max; i++) {

if (strcmp(temp, list[i]) == 0) {

printf("%s\n", temp);

break;

count = 0;

}
if (count > 0) {

temp[count] = '\0';

for (int i = 0; i < max; i++) {

if (strcmp(temp, list[i]) == 0) {

printf("%s\n", temp);

break;

int main() {

char line[100];

printf("Enter the input string: ");

fgets(line, sizeof(line), stdin);

size_t size = strlen(line);

if (size > 0 && line[size - 1] == '\n') {

line[size - 1] = '\0';

scanKey(line);

scanNum(line);

scanOp(line);

scanStr(line);

scanChar(line);

scanPunct(line);

return 0;

}
Output:

You might also like