View Full Version: Checking individual characters in a string

C++ Learning Community > win32 api C++ programming > Checking individual characters in a string


Title: Checking individual characters in a string


Dragon - July 3, 2003 05:45 PM (GMT)
Is there a way to check individual characters of a string? For example, if I have the string 'hello', how can I change individual characters of that string? So with the 'hello' string, how can I change, for example, 'e' to another character? Thanks.

Xception - July 3, 2003 05:58 PM (GMT)
That's easy, a string in C is only an array of characters.
CODE

#include <iostream.h>
#include <conio.h>

int main() {
char text[]="Hello";
text[1]='a';
cout<< text;
getch();
return 0;
}

This translates "Hello" into German "Hallo" :D

Dragon - July 3, 2003 06:05 PM (GMT)
Hey, thanks Xception. What does 'getch()' do anyways? And are you German? Your website is hosted on a German host, so I assumed that you were German. I've only taken two years of German at school, and will take another year next year.

Xception - July 3, 2003 07:03 PM (GMT)
int getch(void)
Get a character from the console without echo
I only used it so that the console window stays open.
And yes, I'm German.

Dragon - July 3, 2003 08:15 PM (GMT)
Ok, I got that down now. How would I read a line from a file and then store the contents in a string, then do the same thing except with an if statement? I tried to do:

QUOTE
#include <iostream>
#include <fstream>

using namespace std;

int main()
{
  ifstream in("atextfile.txt");
  char text[0];
  if (text[0] == 'e')
  {
    text[0] = 'a';
    cout << text << endl;
  }
  system("PAUSE");
}


That didn't work. A little help, Xception? Anybody?




* Hosted for free by InvisionFree