answersLogoWhite

0


Best Answer
C Solution

To replace a single, specified character with another in a given string, one possibility is ...

char *pszString; /* pointer to string */

int offset; /* offset of desired character */

... initialize pszString and offset

*(pszString+offset) = 'A'; /* or whatever new value you want */

Obviously, this is a simple example, and it does not consider if offset is greater than the size of the array.

If you want to replace every occurence of a character with another, here is another possibility, one that also handles string length ...

char *pszString; /* pointer to string */

char* pszTemp; /* temporary scanning pointer */

char cOldChar; /* character to change */

char cNewChar; /* new character */

... initialize pszString, cOldChar, and cNewChar

for (pszTemp = pszString; *pszTemp != '\0'; pszTemp++) { /* scan */

if (*pszTemp == cOldChar) *pszTemp = cNewChar; /* conditionally replace */

}

Java Solution

// Replace all 'e' characters with 'i' characters in String str

str.replaceAll("e", "i");

User Avatar

Wiki User

14y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Replace a character by another character in a given string?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

How do you write a c program for to replace a character of string either fro mbeginning or ending or at specified location?

The standard C library includes two simple utilities to find the first or last occurance of a given character within a given string, strchr() to search from the start and strrchr() for the reverse start from the end. Subject to the chosen search direction, you could use one of these two simple API. Both return a pointer to the location of the matching character within the string, or NULL if no such character is found. Note that this approach assumes a mutable string, a string stored in writeable memory. A string literal is a constant string and not generally mutable (even though some compilers are very casual about this). That is, strchr("the quick brown fox", 'q') will return a pointer to the first 'q', but since the string is a string of constant characters, you shouldn't use the pointer to change the letter found. To search and modify, you'd use string of variable characters, such as one allocated with the malloc() or strdup() standard API, or one created as a char array.


Write a program that stores vowels in an array When you program is given a character it should indicate whether the character is vowel or not?

That's easy to do!This script will get the POST data from an HTML form and check if it is a vowel.


What is string.h?

With the string.h header file you can do special string functions such as ---- strcat() Sticks two strings together, one at the end of another. strncat() Sticks a given amount of characters from two strings together, one at the end of another. strchr() Returns the location of a character in a string from the beginning. strrchr() Returns the location of a character in a string from the end. strcmp() Compares 2 strings and returns the value 0 if both match. strcasecmp() Compares 2 strings ignoring case and returns the value 0 if both match. strncasecmp() Compares only a set number of characters in 2 strings ignoring case. strcpy() Copies one string into another. strncpy() Copies only a set number of characters from one string to another. strlen() Returns the length of the string (Except the final NULL character). The null character is \0 which dignifies the end of a string. strstr() Locates one string inside of another. Remember to use these you MUST include the string.h header file by typing #include <string.h>.


How do you get odd ascii letters from a given string using string copy?

there is no such method using string copy


How did copy string one variable to another variable in body of the program in c?

/*Program to Copy one string to another using pointer.*/#include#includemain(){char a[80],b[80],*pa,*pb;int i=0;clrscr();printf("Given first string ");scanf("%s",a);pa=&a[0];pb=&b[0];while(*pa!='\0'){*pb=*pa;pa++;pb++;}*pb='\0';printf("\nCopied string is");puts(b);}

Related questions

Function finds the occurrence of a given string in another string?

in C: strstr, declared in string.h


What is th function of the charindex?

The CHARINDEX function in SQL is used to find the position of a specific character or substring within a string. It returns the starting position of the substring or character within the given string.


How do you write a c program for to replace a character of string either fro mbeginning or ending or at specified location?

The standard C library includes two simple utilities to find the first or last occurance of a given character within a given string, strchr() to search from the start and strrchr() for the reverse start from the end. Subject to the chosen search direction, you could use one of these two simple API. Both return a pointer to the location of the matching character within the string, or NULL if no such character is found. Note that this approach assumes a mutable string, a string stored in writeable memory. A string literal is a constant string and not generally mutable (even though some compilers are very casual about this). That is, strchr("the quick brown fox", 'q') will return a pointer to the first 'q', but since the string is a string of constant characters, you shouldn't use the pointer to change the letter found. To search and modify, you'd use string of variable characters, such as one allocated with the malloc() or strdup() standard API, or one created as a char array.


Write a program that stores vowels in an array When you program is given a character it should indicate whether the character is vowel or not?

That's easy to do!This script will get the POST data from an HTML form and check if it is a vowel.


What is string.h?

With the string.h header file you can do special string functions such as ---- strcat() Sticks two strings together, one at the end of another. strncat() Sticks a given amount of characters from two strings together, one at the end of another. strchr() Returns the location of a character in a string from the beginning. strrchr() Returns the location of a character in a string from the end. strcmp() Compares 2 strings and returns the value 0 if both match. strcasecmp() Compares 2 strings ignoring case and returns the value 0 if both match. strncasecmp() Compares only a set number of characters in 2 strings ignoring case. strcpy() Copies one string into another. strncpy() Copies only a set number of characters from one string to another. strlen() Returns the length of the string (Except the final NULL character). The null character is \0 which dignifies the end of a string. strstr() Locates one string inside of another. Remember to use these you MUST include the string.h header file by typing #include <string.h>.


How do you check the data given by the user with database values using PHP from MySQL database?

You could use the string replace attribute but you might have to use Javascript, CGI, ASP or another scripting language to do so.


How do you get odd ascii letters from a given string using string copy?

there is no such method using string copy


String?

For the majority of languages, a string is a primary data type that denotes an ordered series of one or more characters (a character is an item that can be input or output - in general, they are displayable in some form, but there are character systems which have non-displayable characters for reasons of history). One character can only be represented by a single 8-bit byte in some languages (like C), which corresponds to an ASCII code entry. A single character (char) and a string (really an array of characters ended by a NULL (zero) character) both have their data types in the programming language C. Other languages, like Python 3, for instance, define a character as any Unicode item ranging in size from one to four bytes. The runtime environment (JRE), made up of the JVM, is a general-purpose, concurrent, object-oriented, class-based programming language called Java. We shall be talking about Java String, a novel idea, in this blog. Each character in a string is a separate unit. A line, however, is an object that represents a series of characters in Java. A string object is made using the a class named String. A String object can be created in given ways: Using a literal string Java, Double quotes are used to produce string literals. For instance: s="Welcome" string; Using a new keyword When creating a Java String, the "new" keyword is used. For instance: s = new String ("Welcome"); It produces two objects (in a heap and the String pool), as well as one reference variable whose value, "s," refers to the object in a heap. Let's now examine the Java String pool concept. Java String Pool A group of Strings that are kept in heap memory collectively comprise the Java String pool. String Pool initially determines whether the item is already present in the pool or not whenever a new object is formed. If it is, the variable receives the same reference back; otherwise, a new object will be generated Java String Methods Java String length(): The Java String length() function provides the string's length information. It gives the total number of characters in the String as a count. Java's compareTo(): The given string is compared to the current string using the Java String compareTo() function. It is a method of the "Comparable" interface that the String class implements. Java String concat(): This method joins a particular string to the end of other string and then outputs the resulting combined string. It is comparable to adding a new string. Java String IsEmpty(): This method determines whether or not the String is empty. It returns true if the java String is Empty and false otherwise. toLowerCase() : All of the characters in the String are converted to lowercase using the Java String toLowerCase() function. Java's toUpper() method: All of the characters in the String are changed to the upper case via the Java String toUpperCase() function. Java String Replace(): This method returns a string with all the old characters or characters in a CharSequence replaced with new characters. contains(): The Java contains() method looks through the string's characters in order. It returns true if the character sequences are detected; otherwise, it returns false. String to CharArray() in Java: This method turns the given Java String into a character array by first calculating the length of the string, including any spaces, and then producing an array of the same name as a char type. String IsEmpty() in Java: This method determines whether or not the String is empty. The String returns true if its length is zero; otherwise, it returns false. StringBuffer and StringBuilder are two utility classes offered by Java. Let's examine what makes these two utility classes distinct from one another: Mutable classes include StringBuffer and StringBuilder. In contrast to StringBuilder operations, which are not thread-safe, StringBuffer operations are synchronized. StringBuffer should be used in the single-threaded environment when many other threads are working on the same String and StringBuilder. StringBuilder performs faster than StringBuffer Because there is no synchronization overhead, That is all there is to know about string for a novice. I hope this article answers your query.


Where can one download character counter software?

Character Counter is an application that counts the number of characters in any given string. There are several places one could find this online such as Tucows Downloads, Any Count, Ginstrom or Practiline.


How do you determine if a given string is palindrome or not?

Reverse the string and compare it to the original. If they match, then it is a palindrome.


Find out the square root for that given number using string?

Halv that given number,Make a square that has that half number as the length of a side.Stretch the string across the diagonal of the square from corner to corner.The length of the string is the square root of the given number


How can you prove the given string by user is array or not in java?

Strings and Arrays are two totally different data types in Java and they will not match with one another.