gpt-3.5-turbo-0301· OpenAI
WildChat-4.8M (AI2)
напиши на c# Разработайте программу, которая получает с консоли строку, и выводит: Её…
- User
напиши на c# Разработайте программу, которая получает с консоли строку, и выводит:
Её длину. Количество слов в ней (используйте split).
- Assistant
using System;
class Program { static void Main(string[] args) { Console.WriteLine("Введите строку: "); string inputStr = Console.ReadLine();
codeint length = inputStr.Length; Console.WriteLine("Длина строки: " + length); string[] words = inputStr.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); int wordsCount = words.Length; Console.WriteLine("Количество слов: " + wordsCount); Console.ReadKey(); }}
- User
напиши простым кодом без сложных команд на c# Разработайте программу, которая получает с консоли строку, и выводит:
Её длину. Количество слов в ней (используйте split).
- Assistant
Вот простой код для решения задачи:
using System;
class Program { static void Main(string[] args) { Console.WriteLine("Введите строку: "); string inputStr = Console.ReadLine();
codeint length = 0; foreach (char c in inputStr) { length++; } Console.WriteLine("Длина строки: " + length); int wordsCount = 0; bool insideWord = false; foreach (char c in inputStr) { if (c == ' ' || c == '\t' || c == '\n') { if (insideWord) { wordsCount++; insideWord = false; } } else { insideWord = true; } } if (insideWord) { wordsCount++; } Console.WriteLine("Количество слов: " + wordsCount); Console.ReadKey(); }}