Jump to content
  • Advertisement

Archived

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

Stashi

quick string question

This topic is 6091 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!