create account

Easy Tutorial: Computer Programming for DUMMIES -- C++ vs C programming (strings) by charlie.wilson

View this thread on: hive.blogpeakd.comecency.com
· @charlie.wilson ·
$23.13
Easy Tutorial: Computer Programming for DUMMIES -- C++ vs C programming (strings)
http://1.bp.blogspot.com/-RW1OPKr2SN8/ViOYD3Es-GI/AAAAAAAABA8/9weSpOSiuew/s1600/diffrence%2Bbetween%2Bc%2Band%2Bc%252B%252B.png
[Image source](http://www.theidlecoder.com/2015/10/difference-between-c-and-cpp.html)

I usually hate these rage faces, but that picture was pretty funny if you are a programming nerd! This is part 15 of my programming tutorials. Here are the rest if you need to catch up:

[**Part 1: Hello World!**](https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming)

[**Part 2: Variables**](https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming-part-2-for-dummies)

[**Part 3: Functions**](https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming-part-3-for-dummies)

[**Part 4: if(), else, else if()**](https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming-for-dummies-not-language-specific-very-helpful-for-beginners)

[**Part 5: Loops**](https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming-for-dummies-not-language-specific-beginner-friendly)

[**Part 6: Arrays**](https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming-for-dummies-beginner-friendly)

[**Part 7: Basic Input/Output**](https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming-for-dummies-p7-very-beginner-friendly)

[**Part 9: Sorting Arrays**](https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming-for-dummies-sorting)

[**Part 10: Random Numbers**](https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming-for-dummies-generating-random-numbers)

[**Part 11: Colored Text in your Terminal**](https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming-for-dummies-colored-text-on-your-terminal)

[**Part 12: Recursion**](https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming-for-dummies-recursion-made-simple)

[**Part 13: Binary Search**](https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming-for-dummies-binary-search)

[**Part 14: 2D Arrays**](https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming-for-dummies-very-easy) 

I want to go in a different direction with this tutorial. Maybe this will become sort of a sub-series to my tutorial series if it is gets good reception. This tutorial will compare the two languages C and C++. C is a very old language. It may be a little bit dated, but it still has very useful applications. C++ was derived from C, so they are very similar. In my opinion, C++ is a bit easier to use. People will warn you to be careful with C, because it will let you figuratively shoot yourself in the foot! I don't think that it is too dangerous as long as you know what you are doing (I won't lie, people have messed up their systems with it, but they were probably doing something risky or stupid -- so don't worry!). Anyways, we are about to look at....

# String Processing!
First, let's look at how to declare a string in C vs C++. 
Note: for string functions in C, include string.h -- to use string as a data type in C++, include string:
C
```
#include<string.h>
```
C++
```
#include<string>
```

In C, a string is a character array:
```
char s[7];
strcpy(s, "string");
```
- Don't forget to allocate memory for the null character too! ('\0')
- char s[7] = {'s','t','r','i','n','g','\0'}; <-- equivalent
- strcpy() is a C function that stores the second argument in the first argument

In C++, a string is still a character array, but we can handle it a little more easily:
```
string s = "string";
```

## Comparisons
in C, here is how two strings are compared:
```
if( (strcmp(s, "string")) == 0)
        printf("Equivalent.\n");
```
- if s is equivalent to "string", "Equivalent." is printed
- strcmp() is a C function that 
    - returns 0 if the strings are equivalent
    - returns a value less than 0 if the first argument is smaller
    - returns a value greater than 0 if the first argument is larger

in C++, it is yet again easier:
```
if(s == "string")
        printf("Equivalent.\n");
```

## Concatenation
... Or putting two strings together.

In C:
```
strcat(s, " and more string");
```
- strcat() is a C function that adds the second argument on to the end of the first
- suppose s still contains "string" (except there was more space allocated to the array)
- after the strcat() call, s now contains "string and more string"

In C++
```
s = s + " and more string";
```
- this has the same effect as the C example above
- again, this is easier!

## Printing a String
In C:
```
printf("%s", s);
```
- for printf(), remember to include stdio.h
- %s is the format specifier for a string
- prints the contents of s

In C++:
```
cout << s;
```
- for cout, include iostream
- you might also want to insert this line of code with the includes: "using namespace std;"
- also prints the contents of s

## Other String functions in C:
http://eitworld.com/c/C33.jpg
[Image source](http://eitworld.com/c/string.php)

Now here are two programs I wrote that do the exact same thing. One is in C, and the other is in C++.

C
```
#include<stdio.h>
#include <string.h>

int main()
{
	char s[50];
	char s1[6] = {'H','e','l','l','o','\0'};
	char s2[8];
	strcpy(s2, " world!");
	strcat(s1, s2);
	strcat(s, s1);
	printf("%s\n\n", s);
	
	if( !(strcmp(s1, s2)) )
		printf("s1 is equivalent to s2\n\n");
	else
		printf("s1 is NOT equivalent to s2\n\n");

	return 0;
}
```

C++
```
#include<iostream>
#include<string>
using namespace std;

int main()
{
	string s;
	string s1 = "Hello";
	string s2 = " world!";
	s = s1 + s2;
	cout << s << endl << endl;

	if(s1 == s2)
		cout << "s1 is equivalent to s2\n\n";
	else
		cout << "s1 is NOT equivalent to s2\n\n";

	return 0;
}
```

Output for C:
```
[cmw4026@omega test]$ gcc string.c
[cmw4026@omega test]$ ./a.out
Hello world!

s1 is NOT equivalent to s2

[cmw4026@omega test]$
```

Output for C++:
```
[cmw4026@omega test]$ g++ string.cpp
[cmw4026@omega test]$ ./a.out
Hello world!

s1 is NOT equivalent to s2

[cmw4026@omega test]$
```

Now that we are done, I bet you are thinking, "Why the heck does anyone use C instead of C++!? It is so much harder!!" C is a bit more difficult sometimes, but not always! Basic i/o and string processing are just a few things that are more difficult in C. C is still a valuable language to know because it will let you do things that other languages will not! 

I hope this was helpful! Leave any suggestions in the comments!
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , and 8 others
properties (23)
authorcharlie.wilson
permlinkeasy-tutorial-computer-programming-for-dummies-c-vs-c-programming-strings
categoryprogramming
json_metadata{"tags":["programming","tutorial","string","comparison","languages"],"image":["http://1.bp.blogspot.com/-RW1OPKr2SN8/ViOYD3Es-GI/AAAAAAAABA8/9weSpOSiuew/s1600/diffrence%2Bbetween%2Bc%2Band%2Bc%252B%252B.png","http://eitworld.com/c/C33.jpg"],"links":["http://www.theidlecoder.com/2015/10/difference-between-c-and-cpp.html","https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming","https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming-part-2-for-dummies","https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming-part-3-for-dummies","https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming-for-dummies-not-language-specific-very-helpful-for-beginners","https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming-for-dummies-not-language-specific-beginner-friendly","https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming-for-dummies-beginner-friendly","https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming-for-dummies-p7-very-beginner-friendly","https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming-for-dummies-sorting","https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming-for-dummies-generating-random-numbers","https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming-for-dummies-colored-text-on-your-terminal","https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming-for-dummies-recursion-made-simple","https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming-for-dummies-binary-search","https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming-for-dummies-very-easy","http://eitworld.com/c/string.php"]}
created2016-10-04 06:20:18
last_update2016-10-04 06:20:18
depth0
children6
last_payout2016-11-04 11:43:57
cashout_time1969-12-31 23:59:59
total_payout_value17.951 HBD
curator_payout_value5.179 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length6,587
author_reputation18,779,183,062,076
root_title"Easy Tutorial: Computer Programming for DUMMIES -- C++ vs C programming (strings)"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id1,437,842
net_rshares23,630,074,658,941
author_curate_reward""
vote details (72)
@nathwright876 ·
Amazing article, it was good to see the suitle differences between the two.
I've worked with strings in C++ before but not with C.
👍  
properties (23)
authornathwright876
permlinkre-charliewilson-easy-tutorial-computer-programming-for-dummies-c-vs-c-programming-strings-20161004t123025341z
categoryprogramming
json_metadata{"tags":["programming"]}
created2016-10-04 12:29:57
last_update2016-10-04 12:29:57
depth1
children2
last_payout2016-11-04 11:43:57
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length130
author_reputation5,638,692,963,675
root_title"Easy Tutorial: Computer Programming for DUMMIES -- C++ vs C programming (strings)"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id1,440,117
net_rshares5,104,873,709
author_curate_reward""
vote details (1)
@charlie.wilson ·
Thanks! You can actually use any of the C functions in C++ because it is directly derived from C.
👍  ,
properties (23)
authorcharlie.wilson
permlinkre-nathwright876-re-charliewilson-easy-tutorial-computer-programming-for-dummies-c-vs-c-programming-strings-20161004t154735901z
categoryprogramming
json_metadata{"tags":["programming"]}
created2016-10-04 15:47:39
last_update2016-10-04 15:47:39
depth2
children1
last_payout2016-11-04 11:43:57
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length97
author_reputation18,779,183,062,076
root_title"Easy Tutorial: Computer Programming for DUMMIES -- C++ vs C programming (strings)"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id1,441,648
net_rshares3,153,920,342
author_curate_reward""
vote details (2)
@nathwright876 ·
good to know.
👍  
properties (23)
authornathwright876
permlinkre-charliewilson-re-nathwright876-re-charliewilson-easy-tutorial-computer-programming-for-dummies-c-vs-c-programming-strings-20161005t104136786z
categoryprogramming
json_metadata{"tags":["programming"]}
created2016-10-05 10:41:06
last_update2016-10-05 10:41:06
depth3
children0
last_payout2016-11-04 11:43:57
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length13
author_reputation5,638,692,963,675
root_title"Easy Tutorial: Computer Programming for DUMMIES -- C++ vs C programming (strings)"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id1,449,131
net_rshares5,407,847,778
author_curate_reward""
vote details (1)
@therajmahal ·
Freaking awesome man, I love love love this post!
👍  
properties (23)
authortherajmahal
permlinkre-charliewilson-easy-tutorial-computer-programming-for-dummies-c-vs-c-programming-strings-20161004t065311011z
categoryprogramming
json_metadata{"tags":["programming"]}
created2016-10-04 06:53:09
last_update2016-10-04 06:53:09
depth1
children1
last_payout2016-11-04 11:43:57
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length49
author_reputation2,180,543,234,539
root_title"Easy Tutorial: Computer Programming for DUMMIES -- C++ vs C programming (strings)"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id1,437,991
net_rshares5,104,873,709
author_curate_reward""
vote details (1)
@charlie.wilson ·
Thanks a lot!!
👍  
properties (23)
authorcharlie.wilson
permlinkre-therajmahal-re-charliewilson-easy-tutorial-computer-programming-for-dummies-c-vs-c-programming-strings-20161004t154545804z
categoryprogramming
json_metadata{"tags":["programming"]}
created2016-10-04 15:45:51
last_update2016-10-04 15:45:51
depth2
children0
last_payout2016-11-04 11:43:57
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length14
author_reputation18,779,183,062,076
root_title"Easy Tutorial: Computer Programming for DUMMIES -- C++ vs C programming (strings)"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id1,441,633
net_rshares3,019,369,197
author_curate_reward""
vote details (1)
@therajmahal · (edited)
Its kind of like the difference between a Toyota Camry and a Ford Mustang. Both cars will get you to where you need to go, but sometimes you want or need the power and control that the manual transmission, 5.0 liter V8 Coyote engine of the Mustang. At other times, you want the ease of use / simplicity / finesse of a Camry.

In my line of work (game development) we switch between the two all the time, depending on what we need for that particular point in code, since C/C++ usually come as a pair with compilers.
👍  
properties (23)
authortherajmahal
permlinkre-charliewilson-easy-tutorial-computer-programming-for-dummies-c-vs-c-programming-strings-20161004t161255209z
categoryprogramming
json_metadata{"tags":["programming"]}
created2016-10-04 16:12:54
last_update2016-10-04 16:15:09
depth1
children0
last_payout2016-11-04 11:43:57
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length515
author_reputation2,180,543,234,539
root_title"Easy Tutorial: Computer Programming for DUMMIES -- C++ vs C programming (strings)"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id1,441,830
net_rshares5,104,873,709
author_curate_reward""
vote details (1)