I have written this code but it's not working. It is showing some extra characters in the end. Here's the code:
// Program to concate/join two string
#include<stdio.h>
#include<string.h>
main()
{
int i,j,n=0;
char str[100],str2[100],str3[100];
printf("Enter string 1:\n");
gets(str);
printf("Enter string 2:\n");
gets(str2);
for(i=0;str[i]!='\0';i++)
{
str3[i]=str[i];
n=n+1;
}
for(i=0,j=n;str2[i]!='\0';i++,j++)
{
str3[j]=str2[i];
}
printf("The concanted sring is: \n");
puts(str3);
}
Terminate the str3 string with '\0' after you finish the copy loop:
for(i=0,j=n;str2[i]!='\0';i++,j++)
{
str3[j]=str2[i];
}
str3[j] = '\0'; // proper termination of the `str3`.
Otherwise the str3will continue till first random '\0' in the memory is encountered. That is why you get extra characters when you print str3.
Also read this: gets() function in C and
Why is the gets function so dangerous that it should not be used?
Avoid gets() in your programs!
In C language, a string is a null-terminated array of characters.
It is showing some extra characters in the end.
Reason for this is that you are not adding null character at the end of string str3 after concatenating str2 to it. Add a null-character at the end of the concatenated string, like this:
str3[j] = '\0';
Also, you should not use gets(). It has been obsoleted. Instead, use fgets().
Additional:
Follow the good programming practice, make a habit of specifying int as the return type of main function.
You can use one of best string manipulation function "strcat()" to concatenate to strings easily. try using below solution :
#include <stdio.h>
#include <string.h>
int main()
{
char str1[] = "Hello" , str2[] = "There";
//concatenates str1 and str2 and resultant string is stored in str1.
strcat(str1,str2);
puts(str1);
puts(str2);
return 0;
}
Output:
HelloThere
There
Related
I am trying to reverse a string in C with pointers but the output is really weird. The logic seems good but I am not sure why it gets outputted like this. Here is the code:
#include <stdio.h>
#include <string.h>
int main()
{
char str[20], reverse_str[20], *pointer;
int i = 0;
printf("%s", "Enter any string: ");
scanf("%s", str);
pointer = str;
int string_length = strlen(pointer);
//int count = 0;
for (int i = string_length; i > 0; i--){
reverse_str[i -1] = *pointer;
pointer++;
}
printf("%d\n", string_length);
printf("Original string = %s\n", str);
printf("Reversed string = %s\n", reverse_str);
}
The output looks like this:
Enter any string: Hello
Original string = Hello
Reversed string = olleH╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠Hello
This is when the role of null terminator comes in which is often ignored as a silly bit of information by beginners.
In C, every string is terminated by a null character because there needs to be some way to know when a particular string ends starting from its initial location in memory. If you properly initialize the string or put in \0 appropriately then the string can be displayed as is. Otherwise every character in memory starting from the zeroth index of the string will be displayed until it encounters a null character. This is how a printf("%s", str) works, in simple words.
You get that weird output because of this reason. This explains it much better.
Solution:
Add reverse_str[string_length] = '\0'; after you reverse your string using that for loop, so that your resultant string is properly null terminated.
Bonus:
And the reason why you got a considerably sane output is that you were lucky since the compiler allocated str and reverse_str close to each other in a direction such that even if you miss the null terminator on reverse_str you hit the null terminator of str.
To print a string, the string needs a NUL-terminator \0 but reverse_str doesn´t have one.
You need to set a \0 at the end of revers_str, like:
reverse_str[string_length] = '\0';
to make it print the right output.
Here's my code
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
int i,j,n;
scanf("%d",&n);
n*=n;
char str[n];
for(i=0;i<n;i++){
str[i]='*';
}
printf("%s",str);
printf("\n%d",strlen(str));
return 0;
}
I input 2 and this is the output I got:
2
****ú#
7
In line 2 it has some weird characters that show in console (U+0013 or control-S between ú and #), but it didn't show here.
Could you explain this to me?
printf("%s", str) assumes that str points to a NUL-terminated char array (aka "C string"). str is not NUL terminated in your case, so printf is running off the end of the buffer, resulting in undefined behavior.
To fix this you need to do two things:
Allocate an additional byte for the NUL terminator
NUL-terminate the array when you're finished writing to it
char str[n+1]
for(i=0;i<n;i++){
str[i]='*';
}
str[n] = '\0';
You are printing with %s format specifier which expects a C string(char array with NUL character at the end). You need to make the last character \0 to make printf recognize the end of the string and stop printing. So allocate one more character in the array and set the last character to \0.
char str[n+1];
str[n] = '\0`;
You need to NUL-terminate your string, like so:
char str[n+1]; /// <<<
for(i=0;i<n;i++){
str[i]='*';
}
str[n]='\0'; /// <<<
I'm new in programming in C and now I'm studying strings.
My question is: if I allocate a string using malloc (as in the code below), is the NULL character automatically inserted at the end of the string?
I find an answer in another question here, and it seems that the NULL character is not automatically included.
But here comes the problem: I know functions like strlen don't work if there isn't the NULL character, and in this code I use it and it works. So I think there is \0 at the end of my string, even if I don't write it anywhere.
What's the answer?
Here's the code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char** argv) {
char *stringa1;
int n;
int i;
printf("How many characters in the string? ");
scanf("%d", &n);
stringa1 = (char*) malloc(n*sizeof(char));
printf("Insert the string: ");
scanf("%s", stringa1);
free(stringa1);
return 0;
}
malloc() returns a void* pointer to a block of memory stored in the heap. Allocating with malloc() does not initialize any string, only space waiting to be occupied.To add a null-terminating character, you either have to do this yourself, or use a function like scanf(), which adds this character for you. Having said this, you need to allocate space for this \0 character beforehand.
Your malloc() call should be this instead:
stringa1 = (char*) malloc((n+1)*sizeof(char)); /*+1 for '\0' character */
Note: You don't need to cast return of malloc. For more information, read this.
Another thing to point out is sizeof(char) is 1, so multiplying this in your malloc() call is not necessary.
You also need to check if malloc() returns NULL. This can be done like this:
if (stringa1 == NULL) {
/* handle exit */
Also, you can only use strlen() on a null-terminated string, otherwise this ends up being undefined behaviour.
Once scanf() is called, and the stringa1 contains some characters, you can call strlen() on it.
Additionally, checking return of scanf() is also a good idea. You can check it like this:
if (scanf("%d", &n) != 1) {
/* handle exit */
Your code with these changes:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void) {
char *stringa1 = NULL;
size_t n, slen;
printf("How many characters in the string? ");
if (scanf("%zu", &n) != 1) {
printf("Invalid input\n");
exit(EXIT_FAILURE);
}
stringa1 = malloc(n+1);
if (stringa1 == NULL) {
printf("Cannot allocate %zu bytes for string\n", n+1);
exit(EXIT_FAILURE);
}
printf("Insert the string: ");
scanf("%s", stringa1);
slen = strlen(stringa1);
printf("String: %s Length: %zu\n", stringa1, slen);
free(stringa1);
stringa1 = NULL;
return 0;
}
if I allocate a string using malloc (as in the code below), is the NULL character automatically inserted at the end of the string?
No. malloc() returns a block of uninitialized memory.
I know functions like 'strlen' don't work if there isn't the NULL character, and in this code I use it and it works. So I think there is '\0' at the end of my string, even if I don't wrote it nowhere.
scanf() inserts the null byte ('\0') for you when you use %s format specifier (assuming scanf() succeeded).
From man scanf():
s Matches a sequence of non-white-space characters; the next
pointer must be a pointer to the initial element of a
character array that is long enough to hold the input sequence
and the terminating null byte ('\0'), which is added
automatically. The input string stops at white space or at
the maximum field width, whichever occurs first.
(emphasis mine).
By the way, you should do error checking for scanf() and malloc() calls.
malloc returns pointer to an uninitialized memory extent.
If you want that the memory extent would be initialized by zeroes then you can use another standard function calloc instead of malloc.
Take into account that usually such a question like this
printf("How many characters in the string? ");
imply that the terminating zero is not counted. So you have to allocate one more byte of memory. For example
stringa1 = ( char* )malloc( ( n + 1 ) *sizeof( char ) );
or
stringa1 = ( char* )calloc( n + 1, sizeof( char ) );
In the last case you may apply the function strlen which returns 0 because the memory extent is zero-initialized.
This call of scanf
scanf("%s", stringa1);
is unsafe. It is better to use fgets instead. For example
fgets( stringa1, n + 1, stdin );
This function can append the string with the new line character. To remove it from the string you can write
stringa1[strcspn( stringa1, "\n" )] = '\0';
The definition of "string" in C is a sequence of characters, terminated by a null character.
To allocate memory for a string, count the chracters (e.g. strlen) and add 1 for this terminating null character.
Functions like scanf and strcpy add the null character; a function like strncpy doesn't always do that.
The easy way to achieve this is to include cs50 library.
Just use get_string function:
#include <stdio.h>
#include <cs50.h>
int main(void) {
// input the string to stringa1
char *stringa1 = get_string("Insert the string: ");
// call the string
printf("The string you type was: %s\n", stringa1);
return 0;
}
Sample output:
Insert the string: Hello World, I am newbie!
The string you type was: Hello World, I am newbie!
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I've been trying to learn C programming by reading a textbook, but am confused about how strings and substrings work.
I have an idea of what strings and substrings are from java, but can't figure out the syntax in C.
Here's a question from the book that I thought might be easy, but I can't get it.
Write and test a function hydroxide that returns a 1 for true if its string argument ends in the substring OH.
It recommends testing the function with KOH and NaCl.
Also, how would I remove and add letters at the end of the string?
Like, if for some reason I wanted to change NaCl to NaOH?
Any help and explanations would be really appreciated.
ETA:
I guess what I'm most confused on is how to make the program look at the last two letters in the string and compared them to OH.
I'm also not sure how to pass strings to functions.
String is a sequence of characters that ends with special null-terminated character '\0'. If there is no \0, functions that work with string won't stop until the \0 symbol is found. This character may happen in any place after the end of pseudo string (I mean a string without \0) and only then stop.
The following example shows the necessity of this null-terminated character:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char string[] = "Hello!";
printf("original string:\n%s\n\n", string);
memset(string, '-', 5);
printf("memset doesn't affect the last two symbols: '!' and '\\0':\n%s", string);
memset(string, '-', 6);
printf("\n\nmemset doesn't affect the last symbol: '\\0':\n%s\n\n", string);
memset(string, '-', 7);
printf("memset affects all symbols including null-terminated one:\n%s", string);
return 0;
}
/* OUTPUT:
original string:
Hello!
memset doesn't affect the last two characters: '!' and '\0':
-----!
memset doesn't affect the last character: '\0':
------
memset affects all characters including null-terminated one:
-------#↓#
*/
Substring is a char sequence that is in a string. It may be less or equal to the string.
Suppose, "NaOH" is a string. Then substring may be: "N", "a", "O", "H", "Na", "aO", "OH", "NaO", "aOH", "NaOH". To find whether substring is in the string or not you can use strstr function. It's prototype is char * strstr ( char * str1, const char * str2 );.
This code shows this function's results:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char *ptrCh = NULL;
ptrCh = strstr("hello", "h");
printf("ptrCh: %p\n", ptrCh);
printf("%s\n\n", ptrCh);
ptrCh = strstr("hello", "z");
printf("ptrCh: %p\n", ptrCh);
printf("%s\n\n", ptrCh);
return 0;
}
/* OUTPUT:
ptrCh: 00403024
hello
ptrCh: 00000000
(null)
*/
As for the first printf, it prints characters beginning from the position of 'h' and when it reaches null-terminated character, which is next after 'o', it stops, exactly as in previous program.
To make your program more interactive, you can declare array and then a pointer to it. Array size must be enough to store the longest formula. Suppose, 100 will be enough:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char buf[100] = {0};
char *ptr = &buf[0];
scanf("%s", ptr);
// printf() gets a pointer as argument
printf("%s\n", ptr);
// printf() gets also a pointer as argument.
// When you pass arrays name without index to a function,
// you pass a pointer to array's first element.
printf("%s", buf);
return 0;
}
And as for rewriting letters in the end of the string. Here is a small program that does it. Pay attention at comments:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char buf[100] = {0};
char formula[100] = {0};
char compound[100] = {0};
char *ptr = &buf[0];
char *pFormula = &formula[0];
char *pCompound = &compound[0];
printf("Enter formula: ");
scanf("%s", pFormula);
printf("Enter chemical compound: ");
scanf("%s", pCompound);
// Copying the first chemical elements without the last
// several that will be replaced by another elements.
strncpy(ptr, pFormula, strlen(pFormula) - strlen(pCompound));
// Adding new compound to the first elements.
// Function also adds a null-terminated character to the end.
strncat(ptr, pCompound, strlen(pCompound));
printf("The new chemical compound is: ");
printf("%s", ptr);
return 0;
}
/* OUTPUT:
Enter formula: NaOH
Enter chemical compound: Cl
The new chemical compound is: NaCl
*/
In C, we use null-terminated strings. That is the "invisible", 0 value. Not ASCII "0", but the zero value, like 8-bit 0x00. You can represent it in literal text with '\0' or "\0" or unquoted 0, however, in a literal string it is redundant because most functions like strcmp() or strstr() or strcat() all expect and work with null terminated strings. Null char is the stops sign for the C standard string functions.
One easy way to implement this with C library calls is to test for existence of the substring and then test that substring's length, which verify it is at end of string.
Assume buf is some big string buffer, char buf[1024] and char *temp is a temporary variable.
temp = strstr(buf, "OH") returns the pointer to "OH" if exists in buf at any offset.
strlen(temp) Get length of temp, if at end of string, it will be 2 (doesn't include null terminator), so if the original string is "OHIO" or "SOHO" it wont match because it'll be 4 and 3 respectively.
The above is the core of the code, not the full robust implementation. You need to check for valid return values, etc.
char buf[1024];
char *temp;
strcpy(buf, "NaOH");
if((temp = strstr(buf, "OH")) != 0)
{
// At this point we know temp points to something that starts with "OH"
// Now see if it is at the end of the string
if(strlen(temp) == 2)
return true; // For C99 include stdbool.h
return false;
}
You could get obscure, and check for the null terminator directly, will be a smidge quicker. This code is safe as long as it is inside the if() for strstr(), otherwise never do this if you don't know a string is a least N characters long.
if(temp[2] == '\0')
return true; // For C99 include stdbool.h
As far as appending to a string, read the docs on strcat. Keep in mind with strcat, you must have enough space already in the buffer you are appending into. It isn't like C++ std::string or Java/C# string where those will dynamically resize as needed. In C, you get to do all of that yourself.
I started following tutorials on C, and then I (from scratch) attempted to do a program where the program chooses a word and you have to guess letters, until you find out the word or run out of attempts.
But I am stuck at the string part, really weird :
srand(time(NULL));
char pWord[6][15] = {"chocolate","peanut","bowling","helicopter","school","controller"}; // Possible words
int repeat=0;
int rNum = rand()%6;
char solution[strlen(pWord[rNum])];
while(repeat<strlen(pWord[rNum])) {
solution[repeat]=pWord[rNum][repeat];repeat++;
}
printf("Answer : %s", solution); printf("\n");
printf("R Answer : %s", pWord[rNum]); printf("\n");
printf("R length : %i", strlen(pWord[rNum])); printf("\n");
strcpy(solution,pWord[rNum]);
For bowling it is fine, but for others it adds weird special characters at random.
I have no idea why this is happening ( i come from java, somewhere lazy and easy ).
In C, the string ends with null character '\0'. So when you declare the character string solution, you need to add one to the length of the char array since strlen() function doesn't count the null character at the end.
char solution[strlen(pWord[rNum])+1];
Then after the while loop, you need to assign the '\0' to the last element of the char array:
while(repeat<strlen(pWord[rNum])) {
solution[repeat]=pWord[rNum][repeat];
repeat++;
}
solution[repeat]='\0';
A better way to do this string copying is to use strcpy() function instead of the while loop:
strcpy(solution, pWord[rNum]);
This way you don't have to assign the null character to the last character. This function does it for you.