CD Experiment 1 to 10-1
CD Experiment 1 to 10-1
int main() {
string str;
cout << "Enter a string: ";
cin >> str;
if (isKeyword(str)) {
cout << str << " is a C++ keyword.\n";
} else {
cout << str << " is not a C++ keyword.\n";
}
return 0;
}
Output :-
Enter a string: break
break is a C++ keyword.
Experiment – 2
Aim :- Program to identify whether a given Line is Comment or not
Program:-
#include <iostream>
#include <string>
using namespace std;
bool isComment(const string& line) {
if (line.substr(0, 2) == "//") {
return true; // Single-line comment
}
if (line.substr(0, 2) == "/*" && line.substr(line.size() - 2, 2) == "*/") {
return true; // Multi-line comment }
return false;
}
int main() {
string line;
cout << "Enter a line: ";
getline(cin, line);
if (isComment(line)) {
cout << "The line is a comment.\n";
} else {
cout << "The line is not a comment.\n";
}
return 0;
}
Output:-
Enter a line: //
The line is a comment.
Experiment – 3
Aim :- Program to validate an Identifier
Program :-
#include <iostream>
#include <cctype>
#include <string>
using namespace std;
bool isValidIdentifier(const string& str) {
if (!isalpha(str[0]) && str[0] != '_') {
return false; // Must start with an alphabet or underscore
}
for (size_t i = 1; i < str.size(); i++) {
if (!isalnum(str[i]) && str[i] != '_') {
return false; // Must contain only alphanumeric characters or
underscores
}
}
return true;
}
int main() {
string str;
cout << "Enter an identifier: ";
cin >> str;
if (isValidIdentifier(str)) {
cout << str << " is a valid identifier.\n";
} else {
cout << str << " is not a valid identifier.\n";
}
return 0;
}
Output:-
Enter an identifier: my_variable
my_variable is a valid identifier.
Experiment – 4
Aim :- Program to Count total number of Words, Blank spaces in a String
Program:-
#include <iostream>
#include <string>
#include <ctype>
using namespace std;
int main() {
string str;
cout << "Enter a string: ";
getline(cin, str);
int wordCount = 0, spaceCount = 0;
bool inWord = false;
for (char ch : str) {
if (isspace(ch)) {
spaceCount++;
inWord = false;
} else if (!inWord) {
wordCount++;
inWord = true;
}
}
cout << "Total words: " << wordCount << endl;
cout << "Total blank spaces: " << spaceCount << endl;
return 0;
}
Output:-
Enter a string: hello world
Total words: 2
Total blank spaces: 1
Experiment – 5
Aim :- From a given vertex in a weighted connected graph, find shortest
paths to other vertices using Dijkstra's algorithm.
Program :-
#include <iostream>
#include <string>
using namespace std;
int main() {
string expression;
cout << "Enter an expression: ";
getline(cin, expression);
int operatorCount = 0;
for (char ch : expression) {
if (ch == '+' || ch == '-' || ch == '*' || ch == '/' || ch == '%') {
operatorCount++;
}
}
cout << "Total number of operators: " << operatorCount << endl;
return 0;
}
Output:-
Enter an expression: 5 + 3 * 2 - 7 / 4
Total number of operators: 4
Experiment – 6
Aim :- Program to Count the Total Number of Keywords in a String.
Program:-
#include <stdio.h>
#include <string.h>
#include <ctype.h>
// List of C keywords
const char *keywords[] = {
"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 keywordCount = sizeof(keywords) / sizeof(keywords[0]);
// Function to check if a word is a C keyword
int isKeyword(const char *word) {
for (int i = 0; i < keywordCount; i++) {
if (strcmp(word, keywords[i]) == 0) {
return 1; // It's a keyword
}
}
return 0; // Not a keyword
}
int main() {
char str[200];
char word[50];
int keywordCounter = 0;
int j = 0;
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
for (int i = 0; str[i] != '\0'; i++) {
if (isalpha(str[i])) {
word[j++] = str[i];
} else if (j > 0) {
word[j] = '\0'; // Null-terminate the word
if (isKeyword(word)) {
keywordCounter++;
}
j = 0; // Reset for the next word
}
}
// Check the last word
if (j > 0) {
word[j] = '\0';
if (isKeyword(word)) {
keywordCounter++;
}
}
printf("Total number of keywords: %d\n", keywordCounter);
return 0;
}
Output:-
Enter a string: if, int, float a =0;
Total number of keywords: 3
Experiment – 7
Aim :- Program to Count total number of Vowels and Consonants in a
String
Program:-
#include <stdio.h>
int main() {
char str[100];
int vowels = 0, consonants = 0;
printf("Enter a string: ");
gets(str); // Read the input string
for (int i = 0; str[i] != '\0'; i++) {
// Check for vowels
if (str[i] == 'A' || str[i] == 'E' || str[i] == 'I' || str[i] == 'O' || str[i] == 'U' ||
str[i] == 'a' || str[i] == 'e' || str[i] == 'i' || str[i] == 'o' || str[i] == 'u') {
vowels++;
}
// Check for consonants
else if ((str[i] >= 'A' && str[i] <= 'Z') || (str[i] >= 'a' && str[i] <= 'z')) {
consonants++;
}
}
printf("Total number of vowels: %d\n", vowels);
printf("Total number of consonants: %d\n", consonants);
return 0;
}
Output:-
Enter a string: Hello World
Total number of vowels: 3
Total number of consonants: 7
Experiment – 8
Aim :- Program to find FIRST of any Grammar.
Program:-
#include <stdio.h>
#include <string.h>
#include <ctype.h>
char production[10][10];
int n;
void findFIRST(char c, char* result) {
for (int i = 0; i < n; i++) {
if (production[i][0] == c) {
if (!isupper(production[i][2])) {
strncat(result, &production[i][2], 1);
} else {
findFIRST(production[i][2], result);
}
}
}
}
int main() {
char result[10] = "";
char c;
printf("Enter number of productions: ");
scanf("%d", &n);
printf("Enter productions (e.g., E=T+F):\n");
for (int i = 0; i < n; i++) {
scanf("%s", production[i]);
}
printf("Enter the symbol to find FIRST: ");
scanf(" %c", &c);
findFIRST(c, result);
printf("FIRST(%c) = { %s }\n", c, result);
return 0;
}
Output:-
How many number of productions? : 4
Enter production Number 1: E=TD
Enter production Number 2: D=+TD
Enter production Number 3: D=$
Enter production Number 4: T=a