0% found this document useful (0 votes)
20 views10 pages

Strings programs unit III TO UPLOAD

Uploaded by

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

Strings programs unit III TO UPLOAD

Uploaded by

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

#include<stdio.

h>
void main()
{
char str[]="aa\0bb";
printf("%s",str);
}

C program to find the length of a string without using the built-


in function

#include <stdio.h>
void main()
{
char string[50];
int i, length = 0;
printf("Enter a string \n");
gets(string);
/* keep going through each character of the string till its end */
for (i = 0; string[i] != '\0'; i++)
{
length++;
}
printf("The length of a string is the number of characters in it \n");
printf("So, the length of %s = %d\n", string, length);
}
//Program(String reverse without library functions)
#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);
}
/* C program to Reverse a String without using strrev() */

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

int main()
{
char Str[100], RevStr[100];
int i, j, len;

printf("\n Please Enter any String : ");


gets(Str);

j = 0;
len = strlen(Str);

for (i = len - 1; i >= 0; i--)


{
RevStr[j] = Str[i];
j++;

}
RevStr[j] = '\0';

printf("\n String after Reversing = %s", RevStr);

return 0;
}
/* C program to Copy String without using strcpy() */

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

int main()
{
char Str[100], CopyStr[100];
int i;

printf("\n Please Enter any String : ");


gets(Str);

for (i = 0; Str[i]!='\0'; i++)


{
CopyStr[i] = Str[i];
}
CopyStr[i] = '\0';

printf("\n String that we coped into CopyStr = %s", CopyStr);


printf("\n Total Number of Characters that we copied = %d\n", i);

return 0;
}
To Concatenate Two Strings without using strcat() */

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

int main()
{
char Str1[100], Str2[100];
int i, j;

printf("\n Please Enter the First String : ");


gets(Str1);
printf("\n Please Enter the Second String : ");
gets(Str2);

i = 0;
while( Str1[i]!='\0')
{
i++;
}

j = 0;
while( Str2[j]!='\0')
{
Str1[i] = Str2[j];
i++;
j++;
}
Str1[i] = '\0';

printf("\n String after the Concatenate = %s", Str1);

return 0;
}

/* C program to Compare Two Strings without using library


function */

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

int main()
{
char Str1[100], Str2[100];
int result, i;
i = 0;

printf("\n Please Enter the First String : ");


gets(Str1);
printf("\n Please Enter the Second String : ");
gets(Str2);

while(Str1[i] == Str2[i] && Str1[i] == '\0')


{
i++;
}

if(Str1[i] < Str2[i])


{
printf("\n str1 is Less than str2");
}
else if(Str1[i] > Str2[i])
{
printf("\n str2 is Less than str1");
}
else
{
printf("\n str1 is Equal to str2");
}

return 0;
}
SORTING OF NAMES
#include <stdio.h>
#include <string.h>
void main()
{

char name[10][8], tname[10][8], temp[8];


int i, j, n;

printf("Enter the value of n \n");


scanf("%d", &n);
printf("Enter %d names \n", n);
for (i = 0; i < n; i++)
{
scanf("%s", name[i]);
strcpy(tname[i], name[i]);
}

for (i = 0; i < n - 1 ; i++)


{
for (j = i + 1; j < n; j++)
{
if (strcmp(name[i], name[j]) > 0)
{
strcpy(temp, name[i]);
strcpy(name[i], name[j]);
strcpy(name[j], temp);
}
}
}

printf("\n----------------------------------------\n");
printf("Input NamestSorted names\n");
printf("------------------------------------------\n");

for (i = 0; i < n; i++)


{
printf("%s\t\t%s\n", tname[i], name[i]);
}

printf("------------------------------------------\n");

}
//SUM OF DIAGONAL ELEMENTS
#include <stdio.h>
void main ()
{

static int array[10][10];


int i, j, m, n, a = 0, sum = 0;

printf("Enter the order of the matix \n");


scanf("%d %d", &m, &n);

if (m == n )
{

printf("Enter the co-efficients of the matrix\n");


for (i = 0; i < m; ++i)
{
for (j = 0; j < n; ++j)
{
scanf("%d", &array[i][j]);
}
}

printf("The given matrix is \n");


for (i = 0; i < m; ++i)
{
for (j = 0; j < n; ++j)
{
printf(" %d", array[i][j]);
}
printf("\n");
}

for (i = 0; i < m; ++i)


{
sum = sum + array[i][i];
//a = a + array[i][m - i - 1];
}

printf("\nThe sum of the main diagonal elements is = %d\n", sum);


//printf("The sum of the off diagonal elements is = %d\n", a);

else
printf("The given order is not square matrix\n");

PROGRAM TO FIND NUMBER OF vowels, consonant, digit, space


#include <stdio.h>
int main()
{
char line[150];
int vowels, consonant, digit, space;
vowels = consonant = digit = space = 0;
printf("Enter a line of string: ");
fgets(line, sizeof(line), stdin);
for (int 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')
{
++vowels;
}
else if ((line[i] >= 'a' && line[i] <= 'z') || (line[i] >= 'A' && line[i] <= 'Z'))
{
++consonant;
}
else if (line[i] >= '0' && line[i] <= '9')
{
++digit;
}
else if (line[i] == ' ')
{
++space;
}
}
printf("Vowels: %d", vowels);
printf("\nConsonants: %d", consonant);
printf("\nDigits: %d", digit);
printf("\nWhite spaces: %d", space);
return 0;
}

You might also like