BLOGS

Insert and display number in 2d array using c

2D Array in C:

#include<stdio.h>

/* run this program using the console pauser or add your own getch, system(“pause”) or input loop */

int main(int argc, char *argv[]) {

int array[3][3];
int row,col;

for(row=0;row<3;row++){
for(col=0;col<3;col++){
printf(“enter value : “);
scanf(“%d”,&array[row][col]);
}
}

for(row=0;row<3;row++){
for(col=0;col<3;col++){

printf(“%d\t”,array[col][row]); }
printf(“\n”);

}

return 0;
}

check number is palindrome or not in c

/* run this program using the console pauser or add your own getch, system(“pause”) or input loop */

#include
int main()
{
int n, nInteger = 0, remainder, oInteger;

printf(“Enter an integer: “);
scanf(“%d”, &n);

for(oInteger = n; n!=0; n/=10 )
{
remainder = n%10;
nInteger = nInteger*10 + remainder;

}

if (oInteger == nInteger)
printf(“%d is a palindrome.”, oInteger);
else
printf(“%d is not a palindrome.”, oInteger);

return 0;
}

Program in C to convert a string to uppercase

#include
#include

int main()
{
int ctr=0;
char str_char;
char str[100];

printf(“\n Convert a string to uppercase. :\n”);
printf(“———————————–“);
printf(“\n Input a string in lowercase : “);
fgets(str, sizeof str, stdin);
printf(” Here is the above string in UPPERCASE :\n “);
while (str[ctr])
{
str_char=str[ctr];
putchar (toupper(str_char));
ctr++;
}
printf(“\n\n”);
return 0;
}

Program in C to extract a sub-string from a given string

#include
void main()
{
char str[100], sstr[100];
int pos, l, c = 0;

printf(“\n\nExtract a substring from a given string:\n”);
printf(“——————————————–\n”);

printf(“Input the string : “);
fgets(str, sizeof str, stdin);

printf(“Input the position to start extraction :”);
scanf(“%d”, &pos);

printf(“Input the length of substring :”);
scanf(“%d”, &l);

while (c < l) { sstr[c] = str[pos+c-1]; c++; } sstr[c] = '\0'; printf("The substring retrieve from the string is : \" %s\ "\n\n", sstr); }

Program in C to copy one string to another string

#include
#include
#include

void main()
{
char str1[100], str2[100];
int i;

printf(“\n\nCopy one string into another string :\n”);
printf(“—————————————–\n”);
printf(“Input the string : “);
fgets(str1, sizeof str1, stdin);

/* Copies string1 to string2 character by character */
i=0;
while(str1[i]!=’\0′)
{
str2[i] = str1[i];
i++;
}

//Makes sure that the string is NULL terminated
str2[i] = ‘\0’;

printf(“\nThe First string is : %s\n”, str1);
printf(“The Second string is : %s\n”, str2);
printf(“Number of characters copied : %d\n\n”, i);
}

Program in C to count the total number of words in a string in c

#include
#include
#include

#define str_size 100 //Declare the maximum size of the string

void main()
{
char str[str_size];
int i, wrd;

printf(“\n\nCount the total number of words in a string :\n”);
printf(“——————————————————\n”);
printf(“Input the string : “);
fgets(str, sizeof str, stdin);

i = 0;
wrd = 1;

/* loop till end of string */
while(str[i]!=’\0′)
{
/* check whether the current character is white space or new line or tab character*/
if(str[i]==’ ‘ || str[i]==’\n’ || str[i]==’\t’)
{
wrd++;
}

i++;
}

printf(“Total number of words in the string is : %d\n”, wrd-1);
}

Read any Month Number and display Month name in C

#include

void main()
{
int monno;
printf(“Input Month No : “);
scanf(“%d”,&monno);
switch(monno)
{
case 1:
printf(“January\n”);
break;
case 2:
printf(“February\n”);
break;
case 3:
printf(“March\n”);
break;
case 4:
printf(“April\n”);
break;
case 5:
printf(“MAy\n”);
break;
case 6:
printf(“June\n”);
break;
case 7:
printf(“July\n”);
break;
case 8:
printf(“August\n”);
break;
case 9:
printf(“September\n”);
break;
case 10:
printf(“October\n”);
break;
case 11:
printf(“November\n”);
break;
case 12:
printf(“December\n”);
break;
default:
printf(“invalid Month number. \nPlease try again ….\n”);
break;
}
}