0% found this document useful (0 votes)
175 views

C Programs

The document contains code snippets and explanations for various string manipulation programs in C without using library functions, including comparing two strings, reversing a string, concatenating two strings, finding the length of a string, copying one string to another, and a program to check if a number is an Armstrong number. The document provides the code and step-by-step explanations for each program.

Uploaded by

vaishuraji2001
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)
175 views

C Programs

The document contains code snippets and explanations for various string manipulation programs in C without using library functions, including comparing two strings, reversing a string, concatenating two strings, finding the length of a string, copying one string to another, and a program to check if a number is an Armstrong number. The document provides the code and step-by-step explanations for each program.

Uploaded by

vaishuraji2001
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/ 16

C Program to Compare Two Strings Without

Using Library Function


#include<stdio.h>
int main() {
char str1[30], str2[30];
int i;
printf("\nEnter two strings :");
gets(str1);
gets(str2);
i = 0;
while (str1[i] == str2[i] && str1[i] != '\0')
i++;
if (str1[i] > str2[i])
printf("str1 > str2");
else if (str1[i] < str2[i])
printf("str1 < str2");
else
printf("str1 = str2");
return (0);
}
C Program to Copy One String into Other
Without Using Library Function.
#include<stdio.h>

int main() {
char s1[100], s2[100];
int i;
printf("\nEnter the string :");
gets(s1);
i = 0;
while (s1[i] != '\0') {
s2[i] = s1[i];
i++;
}
s2[i] = '\0';
printf("\nCopied String is %s ", s2);
return (0);
}

1 Enter the string : c4learn.com


2 Copied String is c4learn.com

Explanation :

1
2
3
4
5

i = 0;
while (s1[i] != '\0') {
s2[i] = s1[i];
i++;
}
1.Scan Entered String From Left to Right ,
Character by Character.
2.In Each Iteration Copy One Character To
New String Variable.
3.As soon as Source or Original String Ends ,
Process of Coping Character Stops but we
still havent Copied NULL Character into
new String so ,Append Null Character to
New String.
Program : Reverse String Without Using Library
Function [ Strrev ]

#include<stdio.h>
#include<string.h>

int main() {
char str[100], temp;

int i, j = 0;

printf("\nEnter the string :");


gets(str);

i = 0;
j = strlen(str) - 1;

while (i < j) {
temp = str[i];
str[i] = str[j];
str[j] = temp;
i++;
j--;
}

printf("\nReverse string is :%s", str);


return (0);
}

Output :

1 Enter the string : Pritesh


2 Reverse string is : hsetirP
Explanation Of Program :
Firstly find the length of the string using library
function strlen().

1j = strlen(str)-1;
Suppose we accepted String Pritesh then

1 j = strlen(str)-1;
2 = strlen("Pritesh") - 1
3 =7-1
4 =6
As we know String is charracter array and
Character array have character range between 0
to length-1. Thus we have position of last

character in variable j.Current Values of i and j


are

1 i = 0;
2 j = 6;
i positioned on first character and j positioned
on last character. Now we are swapping
characters at position i and j. After
interchanging characters we are incrementing
value of i and decrementing value of j.

1 while (i < j) {
2

temp = str[i];

str[i] = str[j];

str[j] = temp;

i++;

j--;

Program : C Program to Concat Two Strings without


Using Library Function
#include<stdio.h>
#include<string.h>

void concat(char[], char[]);

int main() {
char s1[50], s2[30];

printf("\nEnter String 1 :");


gets(s1);
printf("\nEnter String 2 :");
gets(s2);

concat(s1, s2);
printf("nConcated string is :%s", s1);

return (0);

void concat(char s1[], char s2[]) {


int i, j;

i = strlen(s1);

for (j = 0; s2[j] != '\0'; i++, j++) {


s1[i] = s2[j];
}

s1[i] = '\0';
}
Output of Program :

1 Enter String 1 : Pritesh


2 Enter String 2 : Taral
3 Concated string is : PriteshTaral

Explanation of Code :
Our program starts from main and we are
accepting two strings from user using these
following statements
1 printf("\nEnter String 1 :");
2 gets(s1);
3 printf("\nEnter String 2 :");
4 gets(s2);
Inside the concate() function we are firstly
calculating the size of first string.
1i = strlen(s1);
Now we are iterating 2nd string character by
character and putting each character to the end
of the 1st string.

MATRIX MULTIPLICATION:
#include<stdio.h>
int main() {
int a[10][10], b[10][10], c[10][10], i, j, k;

int sum = 0;
printf("\nEnter First Matrix : n");
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
scanf("%d", &a[i][j]);
}
}
printf("\nEnter Second Matrix:n");
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
scanf("%d", &b[i][j]);
}
}
printf("The First Matrix is: \n");
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
printf(" %d ", a[i][j]);
}
printf("\n");
}
printf("The Second Matrix is : \n");
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
printf(" %d ", b[i][j]);
}
printf("\n");

}
//Multiplication Logic
for (i = 0; i <= 2; i++) {
for (j = 0; j <= 2; j++) {
sum = 0;
for (k = 0; k <= 2; k++) {
sum = sum + a[i][k] * b[k][j];
}
c[i][j] = sum;
}
}
printf("\nMultiplication Of Two Matrices :
\n");
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
printf(" %d ", c[i][j]);
}
printf("\n");
}
return (0);
}

STRING
FUNCTIONS:Length,concatenate,compare,co
py,exit
///fundamental string operation, lenth,
concatenation, compare and copy strings
without string.h
#include<stdio.h>
#include<stdlib.h>
int find_length(char string[]){
int len = 0,i;
for(i = 0; string[i]!='\0'; i++){
len++;
}
return len;
}
void join_strings(char string1[], char
string2[]){
int i, len1, len2;
len1 = find_length(string1);
len2 = find_length(string2);
for(i = len1; i < len1+len2; i++){
string1[i] = string2[i-len1];
}
string1[i] = '\0'; //adding null character at
the end of input
}
/*returns 0 if they are same otherwise
returns 1*/

int compare_strings(char string1[], char


string2[]){
int len1, len2, i, count = 0;
len1 = find_length(string1);
len2 = find_length(string2);
if(len1!=len2)
return 1;
for(i = 0; i < len1; i++){
if(string1[i] == string2[i])
count++;
}
if(count == len1)
return 0;
return 1;
}
void copy_string(char destination[], char
source[]){
int len,i;
len = find_length(source);
for(i = 0; i < len; i++){
destination[i] = source[i];
}
destination[i] = '\0';
}
int main(){
char string1[20], string2[20]; //string
variables declaration with size 20
int choice;
while(1){

printf("\n1. Find Length \n2. Concatenate


\n3. Compare \n4. Copy \n5. Exit\n");
printf("Enter your choice: ");
scanf("%d",&choice);
switch(choice){
case 1:
printf("Enter the string: ");
scanf("%s",string1);
printf("The length of string is %d",
find_length(string1));
break;
case 2:
printf("Enter two strings: ");
scanf("%s%s",string1,string2);
join_strings(string1,string2);
printf("The concatenated string is %s",
string1);
break;
case 3:
printf("Enter two strings: ");
scanf("%s%s",string1,string2);
if(compare_strings(string1,string2)==0){
printf("They are equal");
}else{
printf("They are not equal");
}
break;
case 4:
printf("Enter a string: ");

scanf("%s",string1);
printf("String1 = %s\n");
printf("After copying string1 to string 2\n");
copy_string(string2,string1);
printf("String2 = %s",string2);
break;
case 5:
exit(0);
}
}
return 0;
}
Armstrong Number : When Sum of Cubes of
Digits Of Number Equal to Same Given Number
then the number is called as Armstrong Number
#include<stdio.h>
void main() {
int num, temp, sum = 0, rem;
printf("\nEnter number for checking
Armstrong : ");
scanf("%d", &num);
temp = num;

while (num != 0) {
rem = num % 10;
sum = sum + (rem * rem * rem);
num = num / 10;
}
if (temp == sum)
printf("%d is Amstrong Number", temp);
else
printf("%d is not an Amstrong Number",
temp);
getch();
}

You might also like