Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 8

char[] x = {'0','1','2'};

string s = "010120301";

foreach (char c in s)
{
// check if c can be found within s
}

===========
if (x.Contains(c))
{
//// Do Something
}
========
if (Array.IndexOf(x, c) > -1)
{
// The x array contains the character c
}
=========
if(x.Contains(c)) { ... }
========
string input = "A_123000544654654";
string pattern = "[0-9]+";
System.Text.RegularExpressions.Regex.IsMatch(input, pattern);

===============
c lang

/*
* C Program to Check whether a given Character is present in a
* String, Find Frequency & Position of Occurrence
*/
#include <stdio.h>
#include <string.h>

int main()
{
char a, word[50];
int i, freq = 0, flag = 0;

printf("Enter character: ");


scanf("%c", &a);
printf("Now enter the word: ");
scanf("%s", word);
printf("Positions of '%c' in %s are: ", a, word);
for (i = 0; i < strlen(word); i++)
{
if (word[i] == a)
{
flag = 1;
printf("%d ", i + 1);
freq++;
}
}
if (flag)
{
printf("\nCharacter '%c' occured for %d times.\n", a, freq);
}
else
{
printf("None\n");
}

return 0;
}

$ cc charfrequency.c
$ ./a.out
Enter character: r
Now enter the word: programming
Positions of 'r' in programming are: 2 5
Character 'r' occured for 2 times.

===============

Calender Program in C Programming Language :


Program will accept Year,Month and Date from the user and will display the day of
the month.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include<stdio.h>
#include<conio.h>
#include<math.h>

int fm(int date, int month, int year) {


int fmonth, leap;

//leap function 1 for leap & 0 for non-leap


if ((year % 100 == 0) && (year % 400 != 0))
leap = 0;
else if (year % 4 == 0)
leap = 1;
else
leap = 0;

fmonth = 3 + (2 - leap) * ((month + 2) / (2 * month))


+ (5 * month + month / 9) / 2;

//bring it in range of 0 to 6
fmonth = fmonth % 7;

return fmonth;
}

//----------------------------------------------
int day_of_week(int date, int month, int year) {

int dayOfWeek;
int YY = year % 100;
int century = year / 100;

printf("\nDate: %d/%d/%d \n", date, month, year);

dayOfWeek = 1.25 * YY + fm(date, month, year) + date - 2 * (century % 4);

//remainder on division by 7
dayOfWeek = dayOfWeek % 7;

switch (dayOfWeek) {
case 0:
printf("weekday = Saturday");
break;
case 1:
printf("weekday = Sunday");
break;
case 2:
printf("weekday = Monday");
break;
case 3:
printf("weekday = Tuesday");
break;
case 4:
printf("weekday = Wednesday");
break;
case 5:
printf("weekday = Thursday");
break;
case 6:
printf("weekday = Friday");
break;
default:
printf("Incorrect data");
}
return 0;
}
//------------------------------------------
int main() {
int date, month, year;
printf("\nEnter the year ");
scanf("%d", &year);

printf("\nEnter the month ");


scanf("%d", &month);

printf("\nEnter the date ");


scanf("%d", &date);

day_of_week(date, month, year);

return 0;
}
Output :

1
2
3
4
5
Enter the year 2012
Enter the month 02
Enter the date 29
Date: 29/2/2012
weekday = Wednesday

==============
using System;

namespace Learn
{
class Program
{
static void Main(string[] args)
{
string Str, Revstr = "";
int Length;

Console.Write("Enter A String : ");


Str = Console.ReadLine();

Length = Str.Length - 1;

while (Length >= 0)


{

Revstr = Revstr + Str[Length];


Length--;

Console.WriteLine("Reverse String Is {0}", Revstr);

Console.ReadLine();

}
}
}
OUTPUT
-------------
Enter A String : HELLO
Reverse String Is OLLEH

======================

The below program reverses a string using for loop in C#.NET and prints the
output to Console Window.

Reversing a string using C# for loop

using System;

namespace ProgramCall
{
class StringReverse
{
static void Main()
{

string Str, Revstr = "";


Console.Write("Enter A String : ");
Str = Console.ReadLine();

for (int i = Str.Length - 1; i >= 0; i--)


{

Revstr = Revstr + Str[i];


}

Console.WriteLine("Reverse String Is {0}", Revstr);


Console.ReadLine();
}
}
}

OUTPUT
------------
Enter A String : PROGRAMCALL.COM
Reverse String Is MOC.LLACMARGORP

=============================
/*
* C# Program to Reverse a String without using Reverse function
*/
using System;
class Program
{
static void Main(string[] args)
{
string Str, reversestring = "";
int Length;
Console.Write("Enter A String : ");
Str = Console.ReadLine();
Length = Str.Length - 1;
while (Length >= 0)
{
reversestring = reversestring + Str[Length];
Length--;
}
Console.WriteLine("Reverse String Is {0}", reversestring);
Console.ReadLine();
}
}

Here is the output of the C# Program:

Enter a String : sanfoundry


Reverse String is : yrdnuofnas
============
using System;

namespace Learn
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter a Number");
int numb = int.Parse(Console.ReadLine());
int reverse = 0;
while (numb > 0)
{
int rem = numb % 10;
reverse = (reverse * 10) + rem;
numb = numb / 10;

}
Console.WriteLine("Reverse number={0}", reverse);
Console.ReadLine();
}
}
}

Output
----------
Enter a Number
4567
Reverse number=7654

===============using System;
class myclass
{
static void Main()
{
int fn = 0;
int sn = 1;
int tn = 1;

Console.WriteLine(fn);
Console.WriteLine(sn);
while (true)
{

tn = fn + sn;
if (tn >= 100)
{
break;
}
Console.WriteLine(tn);
fn = sn;
sn = tn;

}
Console.Read();

}
}

Output

0
1
1
2
3
5
8
13
21
34
55
89
===================
Reversing a string using C# for loop

using System;

namespace ProgramCall
{
class StringReverse
{
static void Main()
{

string Str, Revstr = "";


Console.Write("Enter A String : ");
Str = Console.ReadLine();

for (int i = Str.Length - 1; i >= 0; i--)


{

Revstr = Revstr + Str[i];


}

Console.WriteLine("Reverse String Is {0}", Revstr);


Console.ReadLine();
}
}
}

OUTPUT
------------
Enter A String : PROGRAMCALL.COM
Reverse String Is MOC.LLACMARGORP

You might also like