C Programming Examples and Errors
C Programming Examples and Errors
net/projects/embarcadero-devcpp/
int main()
{
char ch;
return 0;
}
//%zu → "print an unsigned decimal integer of type size_t".
[Link] for short
// C program to take input from user and print for short
#include <stdio.h>
#include <limits.h>
int main()
{
short num;
int main()
{
int number;
return 0;
}
[Link] for long
// C program to take input from user and print for long
#include <stdio.h>
#include <limits.h>
int main()
{
long num;
return 0;
}
[Link] for long long
// C program to take input from user and print for long long
#include <stdio.h>
#include <limits.h>
int main()
{
long longnum;
return 0;
}
[Link] for float
// C program to take input from user and print for float
#include <stdio.h>
#include <float.h>
int main()
{
float num;
return 0;
}
[Link] for double
// C program to take input from user and print for double
#include <stdio.h>
#include <float.h>
int main()
{
double num;
return 0;
}
[Link] for long double
// C program to take input from user and print for long double
#include <stdio.h>
#include <float.h>
int main()
{
long double num;
return 0;
}
11.// C program to illustrate syntax error
#include<stdio.h>
void main()
{
int x = 10;
int y = 15;
printf("%d", (x, y)) // semicolon missed
}
Output:
error: expected ';' before '}' token
Output:
warning: division by zero [-Wdiv-by-zero] div = n/0;
Output:
the remainder value is 5
int main() {
greet(); // ❌ error: implicit declaration / undefined reference
return 0;
}
fp = fopen("[Link]", "w");
fprintf(fp, "%d %d", 10, 20);
fclose(fp);
fp = fopen("[Link]", "r");
fscanf(fp, "%d %d", &x, &y);
fclose(fp);
if (num> 0) {
printf("The number is positive.\n");
}
return 0;
}
Input:
5
Output:
The number is positive.
59. if-else Statement
Program:
#include <stdio.h>
intmain() {
int age;
printf("Enter age: ");
scanf("%d", &age);
if (num>= 0) {
if (num % 2 == 0) {
printf("The number is positive and even.\n");
} else {
printf("The number is positive and odd.\n");
}
} else {
printf("The number is negative.\n");
}
return 0;
}
Input:
8
Output:
The number is positive and even
Input:
-5
Output:
The number is negative.
switch (choice) {
case 1:
printf("You chose Addition.\n");
break;
case 2:
printf("You chose Subtraction.\n");
break;
case 3:
printf("You chose Multiplication.\n");
break;
default:
printf("Invalid choice.\n");
}
return 0;
}
Input:
2
Output:
You chose Subtraction.
63. while Loop
Program:
#include <stdio.h>
intmain() {
inti = 1;
while (i<= 5) {
printf("%d ", i);
i++;
}
return 0;
}
Output:
12345
64. for Loop
Program:
#include <stdio.h>
intmain() {
for (inti = 1; i<= 5; i++) {
printf("%d ", i);
}
return 0;
}
Output:
12345
65. do-while Loop
Program:
#include <stdio.h>
intmain() {
inti = 1;
do {
printf("%d ", i);
i++;
} while (i<= 5);
return 0;
}
Output:
12345
66. Goto Statement
Program:
#include <stdio.h>
intmain() {
intnum = 1;
start:
if (num<= 5) {
printf("%d ", num);
num++;
goto start;
}
return 0;
}
Output:
12345
67. break Statement
Program:
#include <stdio.h>
intmain() {
for (inti = 1; i<= 10; i++) {
if (i == 6)
break;
printf("%d ", i);
}
return 0;
}
Output:
12345
68. continue Statement
Program:
#include <stdio.h>
intmain() {
for (inti = 1; i<= 5; i++) {
if (i == 3)
continue;
printf("%d ", i);
}
return 0;
}
Output:
1245
69. Nested Loops
Program:
#include <stdio.h>
int main() {
for (int i = 1; i<= 3; i++) {
for (int j = 1; j <= 3; j++) {
printf("%d,%d ", i, j);
}
printf("\n");
}
return 0;
}
Output:
1,1 1,2 1,3
2,1 2,2 2,3
3,1 3,2 3,3
70. Rectangle Pattern of Stars
Program Code:
#include <stdio.h>
int main() {
int rows = 3, cols = 5;
int main() {
int n = 5;
int main() {
int n = 5;
int main() {
int n = 4;
int main() {
int n = 5;
int main() {
int i = 1;
while (i<= 3) {
int j = 1;
while (j <= 4) {
printf("%d ", j);
j++;
}
printf("\n");
i++;
}
return 0;
}
Sample Output:
1234
1234
1234
#include <stdio.h>
int main() {
for (int i = 1; i<= 3; i++) {
int j = 1;
while (j <= 3) {
printf("%d ", j);
j++;
}
printf("\n");
}
return 0;
}
o/p:
123
123
123
[Link] loop inside while loop
#include <stdio.h>
int main() {
int i = 1;
while (i<= 3) {
for (int j = 1; j <= 3; j++) {
printf("%d ", j);
}
printf("\n");
i++;
}
return 0;
}
o/p:
123
123
123
#include <stdio.h>
int main() {
int i = 1;
do {
for (int j = 1; j <= 3; j++) {
printf("%d ", j);
}
printf("\n");
i++;
} while (i<= 3);
return 0;
}
o/p:
123
123
123
int main() {
int n = 5;
for (int i = 1; i<= n; i++) {
for (int space = 1; space <= n - i; space++) {
printf(" ");
}
for (int j = 1; j <= i; j++) {
printf("* ");
}
printf("\n");
}
return 0;
}
Sample Output:
*
**
***
****
*****
82. Inverted Pyramid Pattern
Program Code:
#include <stdio.h>
int main() {
int n = 5;
for (int i = n; i>= 1; i--) {
for (int space = 1; space <= n - i; space++) {
printf(" ");
}
for (int j = 1; j <= i; j++) {
printf("* ");
}
printf("\n");
}
return 0;
}
Sample Output:
*****
****
***
**
*
83. Diamond Pattern
Program Code:
#include <stdio.h>
int main() {
int n = 5;
int main() {
int n = 5;
for (int i = 1; i<= n; i++) {
for (int j = 1; j <= n; j++) {
if (i == 1 || i == n || j == 1 || j == n)
printf("* ");
else
printf(" ");
}
printf("\n");
}
return 0;
}
Sample Output:
*****
* *
* *
* *
*****
85. Hollow Pyramid Pattern
Program Code:
#include <stdio.h>
int main() {
int n = 5;
for (int i = 1; i<= n; i++) {
for (int space = 1; space <= n - i; space++) {
printf(" ");
}
for (int j = 1; j <= i; j++) {
if (j == 1 || j == i || i == n)
printf("* ");
else
printf(" ");
}
printf("\n");
}
return 0;
}
Sample Output:
*
**
* *
* *
*****
86. Hollow Diamond Pattern
Program Code:
#include <stdio.h>
int main() {
int n = 5;
int main() {
int n = 5;
int main() {
int n = 5;
int main() {
int n = 5;
int temp = n;
while(temp != 0) {
temp /= 10;
count++;
}
temp = n;
while(temp != 0) {
rem = temp % 10;
result += pow(rem, count);
temp /= 10;
}
if(result == original)
printf("Armstrong Number\n");
else
printf("Not Armstrong Number\n");
return 0;
}
Input / Output Example:
Input: 153
Output: Armstrong Number
97. Multiplication Table
Program Code:
#include <stdio.h>
int main() {
int n;
printf("Enter a number: ");
scanf("%d", &n);
for(int i = 1; i <= 10; i++)
printf("%d x %d = %d\n", n, i, n * i);
return 0;
}
Input / Output Example:
Input: 5
Output: Table of 5 from 1 to 10
98. Sum of Digits
Program Code:
#include <stdio.h>
int main() {
int n, sum = 0, rem;
printf("Enter a number: ");
scanf("%d", &n);
while(n != 0) {
rem = n % 10;
sum += rem;
n /= 10;
}
printf("Sum of digits = %d\n", sum);
return 0;
}
Input / Output Example:
Input: 1234
Output: 10
99. Count Digits
Program Code:
#include <stdio.h>
int main() {
int n, count = 0;
printf("Enter a number: ");
scanf("%d", &n);
do {
count++;
n /= 10;
} while(n != 0);
printf("Number of digits = %d\n", count);
return 0;
}
Input / Output Example:
Input: 5678
Output: 4
100. Power of a Number
Program Code:
#include <stdio.h>
int main() {
int base, exp;
long long result = 1;
printf("Enter base and exponent: ");
scanf("%d %d", &base, &exp);
for(int i = 1; i <= exp; i++)
result *= base;
printf("%d^%d = %lld\n", base, exp, result);
return 0;
}
Input / Output Example:
Input: 2 5
Output: 2^5 = 32
101. Factor Count of a Number
Program Code:
#include <stdio.h>
int main() {
int n, count = 0;
printf("Enter a number: ");
scanf("%d", &n);
for(int i = 1; i <= n; i++) {
if(n % i == 0)
count++;
}
printf("Number of factors = %d\n", count);
return 0;
}
Input / Output Example:
Input: 12
Output: 6
102. Print Even Numbers
Program Code:
#include <stdio.h>
int main() {
int n;
printf("Enter limit: ");
scanf("%d", &n);
printf("Even numbers: ");
for(int i = 2; i <= n; i += 2)
printf("%d ", i);
return 0;
}
Input / Output Example:
Input: 10
Output: 2 4 6 8 10
103. Print Odd Numbers
Program Code:
#include <stdio.h>
int main() {
int n;
printf("Enter limit: ");
scanf("%d", &n);
printf("Odd numbers: ");
for(int i = 1; i <= n; i += 2)
printf("%d ", i);
return 0;
}
Input / Output Example:
Input: 9
Output: 1 3 5 7 9
104. Sum of Even and Odd Numbers Separately
Program Code:
#include <stdio.h>
int main() {
int n, evenSum = 0, oddSum = 0;
printf("Enter n: ");
scanf("%d", &n);
for(int i = 1; i <= n; i++) {
if(i % 2 == 0)
evenSum += i;
else
oddSum += i;
}
printf("Sum of even numbers = %d\n", evenSum);
printf("Sum of odd numbers = %d\n", oddSum);
return 0;
}
Input / Output Example:
Input: 10
Output: Sum of even numbers = 30
Sum of odd numbers = 25
[Link] declaration
#include<stdio.h>
void main()
{
int a[]={25,23,21,42};
int i;
for(i=0;i<4;i++)
{
printf("a[%d]=%d\n",i,a[i]);
printf("adress=%u\n",&a[i]);
}
}
Output:
a[0]=25
adress=6684208
a[1]=23
adress=6684212
a[2]=21
adress=6684216
a[3]=42
adress=6684220
106. Array declaration and initialization.
#include<stdio.h>
void main()
{
int i,number[10]={1,5,8};
float num[5]={0.5,15.8,-10};
char name[8]={'n','i','k'};
for(i=0;i<10;i++)
{
printf("number[%d] = %d\n",i,number[i]);
}
for(i=0;i<5;i++)
{
printf("num[%d] = %f\n",i,num[i]);
}
for(i=0;i<8;i++)
{
printf("name[%d] = %c\n",i,name[i]);
}
}
Output:
number[0] = 1
number[1] = 5
number[2] = 8
number[3] = 0
number[4] = 0
number[5] = 0
number[6] = 0
number[7] = 0
number[8] = 0
number[9] = 0
num[0] = 0.500000
num[1] = 15.800000
num[2] = -10.000000
num[3] = 0.000000
num[4] = 0.000000
name[0] = n
name[1] = i
name[2] = k
name[3] =
name[4] =
name[5] =
name[6] =
name[7] =
#include<stdio.h>
void main()
{
int arr[5], i;
printf("enter array elements:");
for(i = 0; i < 5; i++)
{
scanf("%d", &arr[i]);
}
printf("\nPrinting elements of the array: \n\n");
for(i = 0; i < 5; i++)
{
printf("%d ", arr[i]);
}
}
Output:
enter array elements:1 6 3 2 9
16329
#include<stdio.h>
void main()
{
int sum=0, avg, i, marks[10];
for(i=0;i<10;i++)
{
printf("Enter marks:");
scanf("%d",&marks[i]);
}
for(i=0;i<10;i++)
{
sum=sum+ marks[i];
}
avg=sum/10;
printf("\nAverage marks= %d\n", avg);
}
Output:
Enter marks:5
Enter marks:2
Enter marks:8
Enter marks:21
Enter marks:67
Enter marks:32
Enter marks:25
Enter marks:90
Enter marks:32
Enter marks:55
Average marks= 33
#include<stdio.h>
void main()
{
int a[]={25,23,21,42};
int i;
for(i=0;i<4;i++)
{
printf("address=%u, ",&a[i]);
printf("a[%d]=%d %d %d %d \n",i,a[i], *(a+i), *(i+a), i[a]);
}
}
Output:
address=6684208, a[0]=25 25 25 25
address=6684212, a[1]=23 23 23 23
address=6684216, a[2]=21 21 21 21
address=6684220, a[3]=42 42 42 42
#include<stdio.h>
void main()
{
int a[50], i, n, large, small;
printf("How many elements:");
scanf("%d",&n);
#include<stdio.h>
void main()
{
int students[4][2]={101,88,102,97,103,65,104,85};
int i,j;
for(i=0;i<4;i++)
for(j=0;j<2;j++)
{
printf("students[%d][%d] = %d", i,j,students[i][j]);
printf(" address = %u\n", &students[i][j]);
}
}
Output:
students[0][0] = 101 address = 6684192
students[0][1] = 88 address = 6684196
students[1][0] = 102 address = 6684200
students[1][1] = 97 address = 6684204
students[2][0] = 103 address = 6684208
students[2][1] = 65 address = 6684212
students[3][0] = 104 address = 6684216
students[3][1] = 85 address = 6684220
#include<stdio.h>
void main()
{
int arr[10][10], row, col, i, j;
printf("Enter number of row for Array (max 10) : ");
scanf("%d",&row);
printf("Enter number of column for Array (max 10) : ");
scanf("%d",&col);
printf("Now Enter %d*%d Array Elements : ",row, col);
for(i=0; i<row; i++)
{
for(j=0; j<col; j++)
{
scanf("%d",&arr[i][j]);
}
}
printf("The Array is :\n");
for(i=0; i<row; i++)
{
for(j=0; j<col; j++)
{
printf("%d ",arr[i][j]);
}
printf("\n");
}
}
Output:
Enter number of row for Array (max 10) : 5
Enter number of column for Array (max 10) : 2
Now Enter 5*2 Array Elements : 1 2 3 4 5 6 7 8 9 10
The Array is :
1 2
3 4
5 6
7 8
9 10
Output:
Base Address = 6684208
zeroth 1-d address = 6684208
first 1-d address = 6684216
second 1-d address = 6684224
third 1-d address = 6684232
Matrix 1:
1 2 3
4 5 6
7 8 9
Matrix 2:
1 2 3
4 5 6
7 8 9
sum matrix:
2 4 6
8 10 12
14 16 18
#include<stdio.h>
#include<stdlib.h>//for exit() function
void main()
{
int mat1[5][5], mat2[5][5], mat3[5][5], i, j, k, m, n, p, q;
printf("Enter first matrix dimensions: ");
scanf("%d %d",&m, &n);
printf("Enter second matrix dimensions: ");
scanf("%d %d", &p, &q);
if(n!=p)
{
printf("Enter valid dimensions\n");
exit(0);
}
printf("Enter first matrix (%d*%d) : ",m,n);
for(i=0; i<m; i++)
for(j=0; j<n; j++)
scanf("%d",&mat1[i][j]);
Matrix 1:
1 1 1
1 1 1
1 1 1
Matrix 2:
2 2 2
2 2 2
2 2 2
Multiplication matrix:
6 6 6
6 6 6
6 6 6
#include<stdio.h>
void main()
{
int mat1[3][3], mat2[3][3], i, j;
printf("Enter first matrix element (3*3) : ");
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
scanf("%d",&mat1[i][j]);
}
}
printf("\nMatrix 1:\n");
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
printf("%2d ",mat1[i][j]);
}
printf("\n");
}
//Transpose of matrix 1
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
mat2[i][j]=mat1[j][i];
}
}
//print transpose matrix
printf("\nTranspose Matrix:\n");
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
printf("%2d ",mat2[i][j]);
}
printf("\n");
}
}
Enter first matrix element (3*3) : 1 2 3 1 2 3 1 2 3
Matrix 1:
1 2 3
1 2 3
1 2 3
Transpose Matrix:
1 1 1
2 2 2
3 3 3
Transpose Matrix:
1 3 5
2 4 6
#include<stdio.h>
void main()
{
int mat1[3][3], i, j, sum;
sum=0;
printf("Enter first matrix element (3*3) : ");
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
scanf("%d",&mat1[i][j]);
}
}
printf("\nMatrix 1:\n");
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
printf("%2d ",mat1[i][j]);
}
printf("\n");
}
//Trace of matrix 1
for(i=0; i<3; i++)
{
sum=sum+mat1[i][i];
}
//print trace matrix
printf("\nTrace of Matrix1 = %d\n", sum);
}
Output:
Enter first matrix element (3*3) : 1 2 3 4 5 6 7 8 9
Matrix 1:
1 2 3
4 5 6
7 8 9
Trace of Matrix1 = 15
Matrix :
1 2 3
4 5 6
7 8 9
The normal of the matrix is = 16
HELLO
Enter your name: krishna
Your name is krishna.
122. A program to show how gets() and puts() functions are used
#include<stdio.h>
void main()
{
char name[20];
printf("Enter your name: ");
gets(name);
puts("Hello!");
puts(name);
}
Output:
Enter your name: venkata krishna rao
Hello!
venkata krishna rao
int main() {
char str[100];
int i, count = 0;
printf("Enter a string: ");
gets(str); // note: gets() is unsafe, but okay for simple practice programs
return 0;
}
Output:
Enter a string: Hello World
Number of vowels in the string: 3
int main() {
char str[100];
int i;
int a = 0, e = 0, i_count = 0, o = 0, u = 0;
printf("\nVowel frequencies:\n");
printf("A or a: %d\n", a);
printf("E or e: %d\n", e);
printf("I or i: %d\n", i_count);
printf("O or o: %d\n", o);
printf("U or u: %d\n", u);
return 0;
}
Output:
Enter a string: hello world this is great
Vowel frequencies:
A or a: 1
E or e: 2
I or i: 2
O or o: 2
U or u: 0
Output:
6 names:
ram
sham
priya
ramya
raju
ravi
Enter some name:
krishna
ramesh
rajesh
3 names
krishna
ramesh
rajesh
#include <stdio.h>
#include <ctype.h>
int main() {
char str[100];
int vowels = 0, consonants = 0, digits = 0, i;
#include <stdio.h>
#include <string.h>
int main() {
char str[100], rev[100];
int i, len;
len = strlen(str);
for (i = 0; i <len; i++)
rev[i] = str[len - i - 1];
rev[len] = '\0';
printf("Reversed string: %s\n", rev);
return 0;
}
Output:
Enter a string: abcdef
Reversed string: fedcba
128.. Check Whether a String is a Palindrome
#include<stdio.h>
#include<string.h>
int main() {
char str[100];
int i, len, flag = 0;
printf("Enter a string: ");
gets(str);
len = strlen(str);
for (i = 0; i < len / 2; i++) {
if (str[i] != str[len - i - 1]) {
flag = 1;
break;
}
}
if (flag == 0)
printf("The string is a palindrome.\n");
else
printf("The string is not a palindrome.\n");
return 0;
}
Output:
Enter a string: abcdcba
The string is a palindrome.
#include <stdio.h>
int main() {
char str[100];
int i;
printf("Enter a string: ");
gets(str);
for (i = 0; str[i] != '\0'; i++) {
// If character is lowercase (a–z)
if (str[i] >= 'a' && str[i] <= 'z') {
str[i] = str[i] - 32; // Convert to uppercase (ASCII difference = 32)
}
// If character is uppercase (A–Z)
else if (str[i] >= 'A' && str[i] <= 'Z') {
str[i] = str[i] + 32; // Convert to lowercase
}
}
printf("Converted string: %s", str);
return 0;
}
OUTPUT:
Enter a string: abcABC
Converted string: ABCabc
[Link] for 3 – D array
#include <stdio.h>
void main() {
int arr[2][2][2] = { { {1, 2}, {3, 4} }, { {5, 6}, {7, 8} } };
for (int i = 0; i < 2; i++) {
printf("block %d\n",i+1);
for (int j = 0; j < 2; j++) {
for (int k = 0; k < 2; k++) {
printf("arr[%d][%d][%d] = %d\t", i, j, k, arr[i][j][k]);
} printf("\n");
} printf("\n\n\n");
}
}
block 1
arr[0][0][0] = 1 arr[0][0][1] = 2
arr[0][1][0] = 3 arr[0][1][1] = 4
block 2
arr[1][0][0] = 5 arr[1][0][1] = 6
arr[1][1][0] = 7 arr[1][1][1] = 8
#include <stdio.h>
int main() {
int num = 123;
float pi = 3.1415926;
char str[] = "Hello";
// 2. Precision specifier
printf("2. Precision specifier:\n");
printf("Float default: |%f|\n", pi);
printf("Float with 2 decimals: |%.2f|\n", pi);
printf("String with precision 3: |%.3s|\n", str); // only first 3 chars printed
printf("\n");
// 3. Left Justification
printf("3. Left Justification:\n");
printf("Right justified (default): |%10d|\n", num);
printf("Left justified: |%-10d|\n", num);
printf("Left justified string: |%-10s|\n", str);
return 0;
}
Output:
1. Field width specification:
Without width: |123|
With width 10: | 123|
2. Precision specifier:
Float default: |3.141593|
Float with 2 decimals: |3.14|
String with precision 3: |Hel|
3. Left Justification:
Right justified (default): | 123|
Left justified: |123 |
Left justified string: |Hello |
#include <stdio.h>
#include <string.h>
int main() {
char str1[50] = "Hello";
char str2[50] = "World";
char str3[50];
int n = 3;
printf("Initial Strings:\n");
printf("str1 = %s\n", str1);
printf("str2 = %s\n\n", str2);
// strcat(str1, str2)
strcat(str1, str2);
printf("After strcat(str1, str2): %s\n", str1);
// Reset str1
strcpy(str1, "Hello");
// strncat(str1, str2, n)
strncat(str1, str2, n);
printf("After strncat(str1, str2, %d): %s\n", n, str1);
// strcpy(str3, str2)
strcpy(str3, str2);
printf("After strcpy(str3, str2): %s\n", str3);
// strncpy(str3, str2, n)
strncpy(str3, "GoodMorning", n);
str3[n] = '\0'; // Add null terminator manually
printf("After strncpy(str3, \"GoodMorning\", %d): %s\n", n, str3);
printf("str 1 - %s \t str 2 - %s \t str 3 - %s\n",str1,str2,str3);
// strlen(str1)
printf("Length of str1 = %lu\n", strlen(str1));
// strcmp(str1, str2)
if (strcmp(str1, str2) == 0)
printf("strcmp(str1, str2): Strings are equal\n");
else
printf("strcmp(str1, str2): Strings are not equal\n");
// strncmp(str1, str2, n)
if (strncmp(str1, str2, n) == 0)
printf("strncmp(str1, str2, %d): First %d characters are equal\n", n, n);
else
printf("strncmp(str1, str2, %d): First %d characters are not equal\n", n, n);
if (strcmpi(str1, str2) == 0)
printf("strcmpi(str1, str2): Strings are equal (case-insensitive)\n");
else
printf("strcmpi(str1, str2): Strings are not equal (case-insensitive)\n");
if (strcasecmp(str1, str2) == 0)
printf("strcasecmp(str1, str2): Strings are equal (case-insensitive)\n");
else
printf("strcasecmp(str1, str2): Strings are not equal (case-insensitive)\n");
return 0;
}
Output:
Initial Strings:
str1 = Hello
str2 = World
#include <stdio.h>
#include <string.h>
#include <stdlib.h> // for strdup()
int main() {
char str1[100] = "Hello World, Welcome to C Programming world";
char str2[50] = "World";
char ch = 'o';
char *ptr;
// strchr(str1, ch)
ptr = strchr(str1, ch);
if (ptr != NULL)
printf("strchr(str1, '%c') = %s\n", ch, ptr);
else
printf("Character '%c' not found using strchr.\n", ch);
// strrchr(str1, ch)
ptr = strrchr(str1, ch);
if (ptr != NULL)
printf("strrchr(str1, '%c') = %s\n", ch, ptr);
else
printf("Character '%c' not found using strrchr.\n", ch);
// strstr(str1, str2)
ptr = strstr(str1, str2);
if (ptr != NULL)
printf("strstr(str1, \"%s\") = %s\n", str2, ptr);
else
printf("Substring \"%s\" not found using strstr.\n", str2);
return 0;
}
Output:
Original String: Hello World, Welcome to C Programming world
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main() {
char str1[100] = "HelloWorld";
char str2[50] = "Welcome";
char temp[100];
char delim[] = " ,.-";
char *token;
int n = 5;
strrev(temp);
printf("After strrev(str1): %s\n", temp);
// strset(str1, ch) – sets all characters of str1 to ch (non-standard)
strcpy(temp, str1);
strset(temp, '*');
printf("After strset(str1, '*'): %s\n", temp);
str1 = abcdef123
str2 = abcxyz
strspn(str1, str2) = 3
#include<stdio.h>
int main( )
{
char line[150];
int i, v=0, c=0, ch=0, d=0, s=0, o=0;
printf("Enter a line of string:\n");
gets(line);
for(i=0; line[i]!='\0';++i)
{
if(line[i]=='a' || line[i]=='e' || line[i]=='i' || line[i]=='o' || line[i]=='u' || line[i]=='A' ||
line[i]=='E' || line[i]=='I' || line[i]=='O' || line[i]=='U')
++v;
else if((line[i]>='a'&& line[i]<='z') || (line[i]>='A'&& line[i]<='Z'))
++c;
else if(line[i]>='0'&&c<='9')
++d;
else if (line[i]==' ')
++s;
}
printf("Vowels: %d", v);
printf("\nConsonants: %d", c);
printf("\nDigits: %d", d);
printf("\nWhite spaces: %d", s);
return 0;
}
Output:
Enter a line of string:
n v krishna rao
Vowels: 4
Consonants: 8
Digits: 0
White spaces: 3