Check if cyclic string or not - c#

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 hours ago.
Improve this question
Enter a string of characters S. Indicates whether the string S is cyclic or not.
For example S='abcdabcdabcdabcd' is cyclic.
My teacher want me to do this but i think it's too hard. I'm thinking using SplitString but it's just an idea and i dont know how to do. I'm very thankful if someone gives me help

using System;
using System.Collections;
public class MYCLASSNAME {
public static void Main(string[] args){
var str = "abcdabcdabcdabcd";
Console.Write(checkCyclicString(str));
}
static bool checkCyclicString (string str){
var checkString ="";
for(var i=0;i<str.Length-1;i++){
checkString= checkString+ str[i];
var countOfCycles =0;
var haveCycles = false;
for(var j=i+1;j<str.Length-checkString.Length+1;j= j+checkString.Length){
if(checkString==str.Substring(j,checkString.Length)){
haveCycles = true;
countOfCycles++;
}else{
haveCycles =false;
}
}
if(haveCycles && countOfCycles == Math.Ceiling((double)(str.Length/checkString.Length))-1){
return true;
}
}
return false;
}
}
check the cyclic behavior by creating substrigs and comparing them with all the parts of the given string

Related

C# string equals wont work properly [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I get response string "true" from php file...
but ,my function always return false ... here is piece of code
public Boolean authorization(String korisnik, String zaporka) {
using (var client = new WebClient())
{
var values = new NameValueCollection();
values["korisnik"] = korisnik;
values["zaporka"] = zaporka;
var response = client.UploadValues("http://localhost/projectX/autorizacija.php", values);
String responseString = Encoding.Default.GetString(response);
System.Diagnostics.Debug.WriteLine(responseString);
if (responseString.Equals("true"))
{
return true;
}
else
{
return false;
}
}
}
Try:
if (responseString.Trim().Equals("true", StringComparison.InvariantCultureIgnoreCase))
{
return true;
}
else
{
System.Diagnostics.Debug.WriteLine(responseString);
return false;
}
InvariantCultureIgnoreCase = compares strings in a linguistically relevant manner that ignores case
Trim = remove whitespaces
And if false, check output value

Read the lines in a text file after a certain line (c sharp)? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
Let's say I have a text file with this info:
...
Hey
all
this
is
my
question
...
All I want to take are the lines that contain "this" "is" & "my" , I don't know how far they are from the top nor how far they are from the bottom, all I know is that they are in that order and the lines they are surrounded by contain "all" and "question" . I already used a while loop to take everything up to "question", but I don't know how to indicate that it should neglect everything up to "this" (not including it). Could you help me?
the "easiest" route is to convert the file's lines into a list, and use generic collection methods to do what you want.
List<string> AllLines = File.ReadAllLines(yourpath).ToList();
int StartIndex = AllLines.IndexOf(ContainerStartString) + 1;
int EndIndex = AllLines.IndexOf(ContainerEndString) - 1;
List<string> MyLines = AllLines.GetRange(StartIndex, EndIndex);
Been doing it this way for 40 years
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
const string FILENMAME = #"c:\temp\test.txt";
enum State
{
FIND_HEY,
GET_DATA,
FOUND_QUESTION
}
static void Main(string[] args)
{
StreamReader reader = new StreamReader(FILENMAME);
string inputline = "";
State state = State.FIND_HEY;
while ((inputline = reader.ReadLine()) != null)
{
inputline = inputline.Trim();
if (inputline.Count() > 0)
{
switch (state)
{
case State.FIND_HEY :
if(inputline.ToUpper().Contains("HEY"))
{
state = State.GET_DATA;
}
break;
case State.GET_DATA :
if(inputline.ToUpper().Contains("QUESTION"))
{
state = State.FOUND_QUESTION;
}
else
{
Console.WriteLine(inputline);
}
break;
case State.FOUND_QUESTION :
break;
}
}
}
Console.ReadLine();
}
}
}
​

C# Error Index was outside the bounds of the array [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I have this code:
//other streamreader code
var splitsByLine = content.Split('\n');
foreach (var s in splitsByLine)
{
var nameAndSize = s.Split(',');
FileNameAndSizes.Add(nameAndSize[0], Convert.ToInt64(nameAndSize[1]));
}
foreach (var item in FileNameAndSizes)
{
if(File.Exists(Path.Combine(Directory.GetCurrentDirectory(), "cleo", item.Key)))
{
FileInfo f = new FileInfo(Path.Combine(Directory.GetCurrentDirectory(), "cleo", item.Key));
var s1 = f.Length;
if (s1 != item.Value)
{
MessageBox.Show(f.Name + " modified file, please change it to default");
}
}
}
When I run code, i have error in this code:
FileNameAndSizes.Add(nameAndSize[0], Convert.ToInt64(nameAndSize[1]));
Index was outside the bounds of the array. and something about 0x0.. I'm C# newbie, how can i fix it?
It's content value:
AnimModByxXx2o1o.cs, 18616
anims.cs, 18780
Dance.cs, 18661
emergencylights.cs, 32213
fps-de-limiter.cs, 17575
neon.cs, 19019
StreamMemFix.cs, 17560
sun.cs, 17662
WEATHERMENUE.cs, 18437
anim.cs, 17637
anim[0].cs, 20684
anim_0_.cs, 19392
anim1.cs, 18744
anim2.cs, 19012
Anim4.cs, 22900
anim228.cs, 19465
P.S I'm tested it with two files:
First file (current) is getting value from mysql, prints it, and then I need to read value with C# app
Second file (working) is create custom file write to it this content, and it's work ok :?
You need to do simple tests to ensure your values before try to work with them:
http://ideone.com/Iyle5C
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
public class Test
{
public static void Main()
{
//other streamreader code
string[] splitsByLine = new string[] { "AnimModByxXx2o1o.cs, 18616",
"anims.cs, 18780",
"Dance.cs, 18661",
"emergencylights.cs, 32213",
"fps-de-limiter.cs, 17575",
"neon.cs, 19019",
"StreamMemFix.cs, 17560",
"sun.cs, 17662",
"WEATHERMENUE.cs, 18437",
"anim.cs, 17637",
"anim[0].cs, 20684",
"anim_0_.cs, 19392",
"anim1.cs, 18744",
"anim2.cs, 19012",
"Anim4.cs, 22900",
"anim228.cs, 19465",
"" };
if (splitsByLine != null && splitsByLine.Any()) // Test
{
foreach (string s in splitsByLine)
{
var nameAndSize = s.Split(',');
if (nameAndSize != null && nameAndSize.Any() && nameAndSize.Count() > 1) // Test
{
Console.WriteLine(String.Concat(nameAndSize[0], " - ", Convert.ToInt64(nameAndSize[1])));
//FileNameAndSizes.Add(nameAndSize[0], Convert.ToInt64(nameAndSize[1]));
}
}
}
}
}

Get all lines from console within same application [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have an application that writes all sorts of status update and times to the console. I also have an email function that emails to clients. I would like at the end of the application send an email with all lines from the console (same application).
There does not seem to be a function Console.ReadAllLines.
I saw some ideas with GetStdHandle but could get it to work.
Any ideas how I could do this in c# pls?
You can do this by implementing your own TextWriter and Console.SetOut
public class MyWriter : TextWriter
{
private List<string> lines = new List<string>();
private TextWriter original;
public MyWriter(TextWriter original)
{
this.original = original;
}
public override Encoding Encoding
{
get { return Encoding.Default; }
}
public override void WriteLine(string value)
{
lines.Add(value);
original.WriteLine(value);
}
//You need to override other methods also
public string[] GetLines()
{
return lines.ToArray();
}
}
And use it like this
var writer = new MyWriter(Console.Out);
Console.SetOut(writer);
Console.WriteLine("Hello world");
Console.WriteLine("Bye!");
var lines = writer.GetLines();
Reading information back that's already been output to the console is a backwards design. Instead, store the information away in a DB/File/Memory so it can be re-used. continue to display the output as you do. However, when you need to send an email dig the info out of the DB/File/Memory.
It could be done like:
List<string> outputList = new List<string>();
string output = GetOutput();//Run continuously...perhaps in a loop or event trigger..whatever applies
outputList.Add(output);
Console.Writeline(output);
//when ready
SendEmail(outputList);
You could write a wrapper class to take care of it easily.
public class ConsoleWriter()
{
public static List<string> AllLines = new List<string>();
public static WriteConsole(string text)
{
AllLines.Add(text);
Console.Write(text);
}
}
Then read AllLines when you want to send the mail.

How do I get the duration of a video file using C#? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
Any library in C# that allows me to do that?
google result for http://johndyer.name/post/2005/07/22/Retreiving-the-duration-of-a-WMV-in-C.aspx
using WMPLib; // this file is called Interop.WMPLib.dll
WindowsMediaPlayerClass wmp = new WindowsMediaPlayerClass();
IWMPMedia mediaInfo = wmp.newMedia("myfile.wmv");
// write duration
Console.WriteLine("Duration = " + mediaInfo.duration);
// write named attributes
for (int i=0; i<mediaInfo.attributeCount; i++)
{
Console.WriteLine(mediaInfo.getAttributeName(i) + " = " + mediaInfo.getItemInfo(mediaInfo.getAttributeName(i)) );
}
You can try this Extension method.
using Shell32;
public static class Extension
{
public static string GetLength(this FileInfo info)
{
var shell = new ShellClass();
var folder = shell.NameSpace(info.DirectoryName);
var item = folder.ParseName(info.Name);
return folder.GetDetailsOf(item, 27);
}
}
I hope following code snippet will help you :
using WMPLib;
// ...your code here...
var player = new WindowsMediaPlayer();
var clip = player.newMedia(filePath);
Console.WriteLine(TimeSpan.FromSeconds(clip.duration));
and don't forget to add the reference of wmp.dll which will be
present in System32 folder.

Categories

Resources