שאלה בC#
שלום, ניסיתי לכתוב פונקציה שמקבלת מחרוזת ואות, כשהפונקציה תעבור על כל תו ותבדוק האם הוא שווה לאות. במידה והתו שווה לאות, המחרוזת תחליף את האות במחרוזת באותה אות רק בcase אחר. לדוג'- עבור מחרוזת "stringS" והתו s תוחזר המחרוזת Strings ניסיתי לפתור כך: string str = "gioiGag"; char ch = 'g'; Console.WriteLine(str); string res = checkandreplace(str, ch); } static string checkandreplace(string str, char ch) { ch = ch.ToUpper(); string letter = ch.ToString();//for comparing it to other string for (int i = 0; i < str.Length; i++) { if (str == ch) { str = str.ToUpper(); } else if (str != ch) { str = str.ToLower(); if (str != ch) { str = str.ToUppper(); } } return str; } הבנתי שיש בעיה בהחלפת התווים. כיצד אוכל לתקן זאת? תודה מראש
שלום, ניסיתי לכתוב פונקציה שמקבלת מחרוזת ואות, כשהפונקציה תעבור על כל תו ותבדוק האם הוא שווה לאות. במידה והתו שווה לאות, המחרוזת תחליף את האות במחרוזת באותה אות רק בcase אחר. לדוג'- עבור מחרוזת "stringS" והתו s תוחזר המחרוזת Strings ניסיתי לפתור כך: string str = "gioiGag"; char ch = 'g'; Console.WriteLine(str); string res = checkandreplace(str, ch); } static string checkandreplace(string str, char ch) { ch = ch.ToUpper(); string letter = ch.ToString();//for comparing it to other string for (int i = 0; i < str.Length; i++) { if (str == ch) { str = str.ToUpper(); } else if (str != ch) { str = str.ToLower(); if (str != ch) { str = str.ToUppper(); } } return str; } הבנתי שיש בעיה בהחלפת התווים. כיצד אוכל לתקן זאת? תודה מראש