strings and c-strings

Hello again , someone gave me a question to solve for string and it really got me confused.
its something like this .
input: ahmad ahmad
output should be : Ahmad Ahmad
and they asked to use strings for this question .
I couldn't really solve it and I had no internet access authorized so I did my best and here is what I did.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
  #include<iostream>
#include<cstring>
#include<string>
#include<cmath>

using namespace std;

//const char(find) = 'a';
int main()
{
	char a[100];
	cout<<"enter a string of charcters \n";
	cin.getline(a, 100);
	int size =strlen(a);
	for (int i = 0; i < size;i++)
	{
		if (a[i] == 'a')
		{
			a[i] = 'A';
		}
		
	}

	//_strdup(find*_a);
	for (int i = 0; i < size; i++)
		cout << a[i];
	cout << endl;

	system("pause");
	return 0;
}

my question is , is there anyway to use stings for this and not arrays or c-strings , and what are the most important things that I should notice between strings and c-strings .
I did some research on them both but its still pretty confusing .
confusing in usage I mean
Last edited on
The output I had is the same the question wanted , but it asked me to use string and I still don't know what I actually did or if I did it like it was asked to be.
yes.
string a;
a.length(); //strlen
if(a[i] == 'a') //string works just like array here code is fine as is

for (int i = 0; i < size; i++)
cout << a[i];
this is silly, just say cout << a; which works for both c-strings and strings.

you can also uppercase the first letter (a[0]) and then look for spaces and uppercase after spaces, rather than hard code looking for 'a' etc. play with it a bit, see what you can do.

---------
differences in strings and c-strings...
c-strings end in a zero char (the literal integer value zero). you can put the literal value 0 in a string and it will not end it -- strings work off their length.
strings are a class and have methods, as shown already with the length. c-strings are just arrays and you have to call specialized functions on them. the zero terminal in C-strings is critical to maintain and manage if doing hands-on manipulation.

strstr and find are very different for the same sort of result. strstr is a null pointer on missing, else pointer to start of substring. find is the index of the array, and 0 is legal so they have a special value for not found.

strings manage memory for you and hide the pointer aspects and hands-on efforts.
strings have + (strcat) and += (stracat to self) and those also work for just characters so you can build a letter at a time.

I would avoid using c-strings but you should learn the basics. you can do pretty much everything you need to do with them with just a few functions.
strlen, strcat, strstr, sprintf will do about 99% of anything needed. Maybe strtok if you want to go old school on a parser.

you cannot modify the size of a string directly and adjust its end marker yourself. you can change an existing letter, but not add new letters, using [] notation. you have to use functions to do most everything more complex than modification of a single letter.
Last edited on
I see what you mean .
so this should have been working just fine .
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include<iostream>
#include<cstring>
#include<string>
#include<cmath>

using namespace std;

//const char(find) = 'a';
int main()
{
	string a;
	cout << "enter a string of charcters \n";
	getline(cin, a);
	int size = a.length();
	for (int i = 0; i < size; i++)
	{
		if (a[i] == 'a')
		{
			a[i] = 'A';
		}

	}

	//_strdup(find*_a);
	//for (int i = 0; i < size; i++)
		cout << a;
	cout << endl;

	system("pause");
	return 0;
}

I have to use both c-strings and strings got a exam tomorrow xD.
the methods you mentioned are complete news for me :)
all what I knew was strlen.
hmm, you mean I cant modify the amount of data it uses?
but I can insert a \0 anywhere I want to reduce its size.
idk about increasing though.
Last edited on
int size = a.length(); this is redundant. just use a.length() where you had size, and get rid of size entirely. Its correct, its just bloated to have an extra variable, and worse, if (it cannot happen here) you got in the habit of this, and you had a string that could change in size via user input or something, size would become incorrect but length() will always be true.

you can directly modify c-strings as long as your memory is allocated correctly.

char cs[1000] = "hey";
hey[3] = ' ';
hey[4] = 'U';
hey[5] = 0;
cout << cs; //you get "hey U" and it is correct because we put the zero back.

you can't do that with string:
string s = "hey";
s[3] = ' '; //MEMORY ACCESS PROBLEM, undefined behavior and such.
even if the above did not crash
cout << s; //you still get "hey" without the space, because length is still 3.
you have to use a function or operator:
s+= ' '; //ok, length is now 4 and it all works.

the zeros don't work on string. try it.
string s = "the quick brown fox";
s[10] = 0;
cout << s << endl; //what do you get here?
cout << s.length();//and here?
** on my compiler it acts as if 0 char were a space in the console output.

there is a solid string reference on this site. I advise you to play with them in some little programs for a few min to get a feel for it all. try a .find() etc. and exercise the c-string functions I gave you. you will be able to do almost anything with those few, so it should not take too much to throw a couple of code segments together to learn it.
Last edited on
Topic archived. No new replies allowed.