Archived

This topic is now archived and is closed to further replies.

Stashi

quick string question

Recommended Posts

i want to declare a string variable with an unknown length (this string will be entered in by the user when running program) and basically im trying to make a function that reverses the entire string. basically i just need to know how to change a certain index in the string array to a different character.. i've tried this..
    
char *str;
char tmp;
int length;
cin >> str;
length = strlen(str);
for (int x = length-1; x=0; x--) {
    tmp = str[x-1];
    str[x-1] = str[x];
    str[x] = tmp;
}
    
which obviously doesnt work and i know to change the characters i need to make use of some "str" function but i dont know which one or how to...any help? Edited by - Stashi on November 17, 2001 10:41:28 PM

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
  
char str[1024]; // sorry you eighter use a buffer or new your string before trying to store anything there.

char tmp;
int length;
cin >> str;
length = strlen(str);
for (int x = length-1,y = 0; x>y; x--,y++) {
tmp = str[y];
str[y] = str[x];
str[x] = tmp;
}
cout << str;
return 0;

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
are you sure? I complied that, and it does reverse the string, here is the whole thing:

  
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stream.h>

int main()
{
char str[1024];
char tmp;
int length;
cin >> str;
length = strlen(str);
for (int x = length-1,y = 0; x>y; x--,y++) {
tmp = str[y];
str[y] = str[x];
str[x] = tmp;
}
cout << str;
return 0;
}

Share this post


Link to post
Share on other sites
Guest Anonymous Poster

Alternatively you can use this..

char *buffer; //your primary buffer (string)

a) put some string in it

void getstring(char *string)
{
if (string == NULL) string=""; //just checking

buffer = new char[strlen(string)+1]; //set new length
strcpy(buffer, string); //copy string
}


b) to replace a character in string->

void replace(char ch1, char ch2) //ch2 is replacement char
{
char* temp=strchr(buffer, char1);
if (temp != NULL) *temp=char2;
}

Thats all......

(always anonymous)





Share this post


Link to post
Share on other sites
Guest Anonymous Poster

sorry..some corections.

actually char1 & char2 in b) is ch1 & ch2...

(always anonymous)

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
to reverse a string->

length = strlen(str); length--;

for (int x=0; x<(length)/2; x++)
{
str[x]=str[length-x];
str[length-x]=str[x];
}

(anonymous poster)

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
to (anynomous poster).. it definitely wont work

(always anonymous)

Share this post


Link to post
Share on other sites
quote:
Original post by Stashi
i want to declare a string variable with an unknown length (this string will be entered in by the user when running program)...

Not possible (using C). Your C-style strings have to point to valid memory (or you get an access violation), so you''ll need to allocate memory for them before hand. Since you can''t very well ask the user how many characters [s]he is going to enter, you have to reserve an amount of memory that will contain the majority of strings in this instance.

If you''re using C++ you can use STL strings and ignore all of the above.

quote:
...and basically im trying to make a function that reverses the entire string.

See _strrev()

If you''re using C++, look into reverse iterators.

Share this post


Link to post
Share on other sites