Thursday, July 30, 2009

How can I replace a character in a string in C?

if you know where in the string the character is, then its as simple as just writing over it. If you dont know where it is, then you have to do a loop and search through the string to find it, then write over it.





example 1: The string char mystring[] = "Hello World." you want to replace the . with !. your code would be:


mystring[11] = '!';


the . is the 11'th element in the string.





example 2: you want to replace . with !, but dont know where it is in the string. the code would be:





for (int count = 0; count %26lt; sizeof(mystring); count++)


{


if (mystring[count] == '.')


{


mystring[count] = '!';


break;


}


}

How can I replace a character in a string in C?
delete tat character and add another
Reply:You have to loop through each character in that string and replace it with a character of your desire.





Ex:


for(x = 0; x %26lt; strlen(text); x++)


{


text[x] = 'X';


}





For more free C/C++ source codes, visit: http://smartcoder.co.nr








KaBalweg


http://smartcoder.co.nr


How does one display % sign as part of string in C language?

two ways:


1. use escape sequence \%


2. write twice % sign


e.g.


1. printf("I got 79.3%%");


2. printf("I got 79.3\%");

How does one display % sign as part of string in C language?
u have to enter the % symbol within the double quotes
Reply:printf("printing %% here ");





Output: printing % here





Trick: Just use %% to print % in any printf statement...
Reply:There is 'Escape Sequence' for this puprose...


To write special characters you need to put an escape charatcer '\' (backslash) before it.


e.g.





% \%


\ \\


etc.
Reply:printf("%");
Reply:printf("The Percent Sign is: \%");
Reply:"escape it"


like this: \%
Reply:The answer is already out there.... use the escape char "\"


printf("I got 90\%"); gives I got 90%
Reply:%%


How do you convert a long to a string in C? Thanks?

Yes, there is one, clean function available in the C standard library for this purpose....available in every C compiler I've used/owned.





From MSVC++ online help:


--------------------------------------...


Convert a long integer to a string:


char *_ltoa( long value, char *string, int radix );





Return Value: returns a pointer to string. There is no error return.





Parameters:


value -%26gt; Number to be converted


string -%26gt; String result


radix -%26gt; Base of value





Remarks: The _ltoa function converts the digits of value to a null-terminated character string and stores the result (up to 33 bytes) in string. The radix argument specifies the base of value, which must be in the range 2 – 36. If radix equals 10 and value is negative, the first character of the stored string is the minus sign (–).








Example:


/* ITOA.C: This program converts integers of various


* sizes to strings in various radixes.


*/





#include %26lt;stdlib.h%26gt;


#include %26lt;stdio.h%26gt;





void main( void )


{


char buffer[20];


int i = 3445;


long l = -344115L;


unsigned long ul = 1234567890UL;





_itoa( i, buffer, 10 );


printf( "String of integer %d (radix 10): %s\n", i, buffer );


_itoa( i, buffer, 16 );


printf( "String of integer %d (radix 16): 0x%s\n", i, buffer );


_itoa( i, buffer, 2 );


printf( "String of integer %d (radix 2): %s\n", i, buffer );





_ltoa( l, buffer, 16 );


printf( "String of long int %ld (radix 16): 0x%s\n", l,


buffer );





_ultoa( ul, buffer, 16 );


printf( "String of unsigned long %lu (radix 16): 0x%s\n", ul,


buffer );


}








Output:


String of integer 3445 (radix 10): 3445


String of integer 3445 (radix 16): 0xd75


String of integer 3445 (radix 2): 110101110101


String of long int -344115 (radix 16): 0xfffabfcd


String of unsigned long 1234567890 (radix 16): 0x499602d2

How do you convert a long to a string in C? Thanks?
i would use sprintf:





char buffer[20];


sprintf(buffer, "%u", value);





(you can also use snprintf, it's better but not available for every compiler).
Reply:Cast the long as a string





long i;





cout%26lt;%26lt;(string)i;
Reply:I think you can just type cast this. Try:





long toConv = 101;


string longString = (string) toBeConv;





longString is now a string that equals '101'





Sorted.
Reply:i am not sure if there is such a function...





although i know of its reverse (string to a long) through strtol(); and atol();





why not try to make the string variable assume the value of the long variable like this...





long X;


string Y;





Y = X;





but i am not sure if this works... i have studied C and C++ 7yrs ago...





:)

imperial

What is the code for the reversal of string in c?

It should be just a single line.Just use the pointers

What is the code for the reversal of string in c?
void StrReverse4(char *str)


{


if(*str)


{


StrReverse4(str+1);


putchar(*str);


}


}





Try this code it might work
Reply:#include %26lt;stdio.h%26gt;


#include %26lt;string.h%26gt;





int main(void)


{


char str1[] = "This is a test sentence";


char str2[80], *p1, *p2;





/* make p point to end of str1 */


p1 = str1 + strlen(str1) - 1;





p2 = str2;





while(p1 %26gt;= str1)


*p2++ = *p1--;





/* null terminate str2 */


*p2 = '\0';





printf("%s %s", str1, str2);





return 0;


}





Sorry mate, got the entire program for your question! :)


Write a program to get permutations of a string in c++?

it may be of any order plz help me

Write a program to get permutations of a string in c++?
Are you trying to display all the possible permutation of a string?





If so, then you can do it by using a recursive function that temporarily stores a copy of the string.





Break the string down into the chars and then change it around.
Reply:00101011
Reply:i think you cant get permutations by using factorials with numbers, transfer that into strings for permutations, its the same logic.








int factorial(int n){ if(n == 0)return 1;else n * factorial(n-1);}


How can I have a vaiable hold a string in C?

Something like this (but this doesn't work):





char name='dotan';


printf("%s", name);

How can I have a vaiable hold a string in C?
use char name[]='dotan';
Reply:char name[6]={'d','o','t','a',''n};


printf("%s", name)
Reply:Use Array of Characters char[]
Reply:char *str = "HELLO";


its a simplest way for the variable to hold a string in C.Over here str points to "HELLO", more over for String manipulation u can include "String.h".Now You can use it as


printf("%s",str);


Help with C++ code?

Im supposed to take a sentence ending with a '.' and determine which words have 3 or more different vowels. It should also use the isVowel function(which is below). I can determine how many vowels are in the string by the following code however i have no idea how to split up the string into words and then analyze each word nor how to determine if it has 3 DIFFERENT vowels. The program must use files. Can anyone help me. Im new to c++. Thnx.





#include %26lt;string%26gt;


#include %26lt;fstream%26gt;


using namespace std;





int isVowel(char letter)//required


{


switch(letter)


{


case 'a':


case 'e':


case 'i':


case 'o':


case 'u': return 1;break;


default: return 0;break;


}


}





void main()


{


string line;


int count=0, vowel;


char currentLetter;





ifstream data("input.dat");


ofstream results("output.dat");





while ( (currentLetter = data.get()) != EOF)


{


line += currentLetter;





vowel = isVowel(currentLetter);


if(vowel)


count++;


}





results %26lt;%26lt; line;


results %26lt;%26lt; count;


}

Help with C++ code?
Let's break it down into even smaller pieces. Splitting up a sentence into separate words can be done by writing a loop that looks for the space character.





At the moment you have a loop that reads a single character from input and checks to see if it's a vowel. You could modify this so that you read characters and store them in an array until you hit a space. When you hit the space, you process the word that is now stored in the array.





Checking for three different vowels could be done by building a very simple little data structure that records whether each possible vowel has occurred in a word. From this, it will be easy to tell if at least three different vowels have occurred.





Okay, I haven't given all the details because this is obviously an assignment, but hopefully this will get you started. Also, you should begin to see the value of breaking problems down into smaller pieces.
Reply:%26gt; Im supposed to take a sentence ending with a '.' and determine which words have 3 or more different vowels.


Use [cin.getline] function with the '.' as delimiter.


Then make a custom function to break up the sentence into words. This is accomplished using a 2d array of strings.





%26gt; It should also use the isVowel function(which is below). I can determine how many vowels are in the string by the following code however i have no idea how to split up the string into words


I recommend you go through a proper textbook [Herbert Schildt's C++: The Complete Reference is a very good and comprehensive book, and the chapter on strings is EXACTLY what you need.]





%26gt; and then analyze each word nor how to determine if it has 3 DIFFERENT vowels. The program must use files. Can anyone help me. Im new to c++. Thnx.


Once again, first you have to be thorough with the syntax. Then go through the problem in logical steps. Each 'step' becomes a function for you. [This is not Object Oriented Programming, but it doesn't really matter for this particular problem]

elephant ear