Click here to Skip to main content
15,890,123 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
string str="kusuma";

1)in place of "u" the given word has to replace
2)In place of "u" the given letter has to replace
Posted
Updated 23-Sep-10 12:29pm
v2
Comments
Yusuf 23-Sep-10 18:41pm    
Hmmm.... unable to do your own homework, huh?

C#
string word = "kusuma";
char c = 'u';
string newword=String.Empty;
string new_string = "x";
int i = 0;
do
{
    if (word[i] == c)
        newword += new_string;
    else
        newword += word[i];

    i++;
}
while (i < word.Length);

Console.WriteLine(newword);
Console.ReadLine();
 
Share this answer
 
Comments
DavidKiryazi 22-Sep-10 4:39am    
Cool! didn't even think about not using an API function to acheive it.. haha
This is homework, and we don't do homework for people.

0) Nobody working as a programmer for a living is going to do it without using Replace. The requirement is ludicrous.

1) This is such a simple programming task that you should be able to perform it without any help. If you don't learn to analyze a problem, how are you going to learn anything with regards to programming?

2) If you're going to be this lazy throughout your doubtlessly brief career, you should consider a line of work that doesn't require a lot of cerebral mobility, like applying glue to envelop flaps.
 
Share this answer
 
Comments
DavidKiryazi 22-Sep-10 9:17am    
lol @ 2)
Hi,

this is one of a few methods

C#
string word = "kusuma";
char c = 'u';
string new_string = "x";
while (word.IndexOf(c) != -1)
{
   int index = word.IndexOf(c);
   word = word.Remove(index, 1); // remove one char
   word = word.Insert(index, new_string);
}


Hope it helps:)

Dave
 
Share this answer
 
Comments
Baji Jabbar 22-Sep-10 4:02am    
good call. But the question seems a academic one. Using Remove and Insert functions do not fit. Using array and replacing it is what it deserves.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900