Reputation: 396
Here is a simple code of reversing the string in C the last printf statement prints the reverse order , but with a question mark at the end, while I just want to print the reversed string not the question mark How do I fix it?
#include<stdio.h>
#include<string.h>
int main(){
char new_string[100];
char string[100];
scanf("%s",string);
printf("original_number = %s\n",string);
int i;
int l = strlen(string)-1;
for(i = 0; i<=l; i++){
new_string[i] = string[l-i];
printf("%c\n",new_string[i]);
}
printf("rev_number = %s\n",new_string);
}
input : abcd
output: original_number = abcd
d
c
b
a
rev_number = dcba?
Upvotes: 1
Views: 12179
Reputation: 6407
If the question is "How to add '?' to the end of string?" I have to answers:
1) use strcat
to add one more character, e.g.:
#include<stdio.h>
#include<string.h>
int main(){
char new_string[101] = {0}; // +1 to be sure that enough place for '?' will be available
// {0} to init empty string
char string[100];
scanf("%s", string);
printf("original_number = %s\n", string);
int i;
int l = strlen(string) - 1;
for (i = 0; i <= l; i++){
new_string[i] = string[l - i];
printf("%c\n", new_string[i]);
}
strcat(new_string, "?");
printf("rev_number = %s\n", new_string);
}
2) put ?
and add character \0
after the loop end, e.g.:
#include<stdio.h>
#include<string.h>
int main(){
char new_string[101]; // +1 to be sure in place for '?'
char string[100];
scanf("%s", string);
printf("original_number = %s\n", string);
int i;
int l = strlen(string) - 1;
for (i = 0; i <= l; i++){
new_string[i] = string[l - i];
printf("%c\n", new_string[i]);
}
// add one more char
new_string[i] = '?';
// set the string end
new_string[i + 1] = '\0';
printf("rev_number = %s\n", new_string);
}
Pay attention, that if you fill all the array with 0
as char new_string[101] = {0};
, you do not need to add end of string with new_string[i + 1] = '\0';
, so the second variant can be shorter if initialization is as in the first one
For both snippets I have the same output
UPDATE:
One more advice on working with strings. When input is made with scanf
and you know how much characters can be stored in your array of chars use the following approach to prevent violation of the array boundaries
char string[10]; // if you have 10 bytes
scanf("%9s", string); // ask not more than 9 characters
remember that you need one more byte for '\0' (null terminator).
Upvotes: 0
Reputation: 1735
the problem is you are taking "end of string" as the last input in for loop.you do not need the variable "l". you can do some think like that
int main()
{
char new_string[100];
char string1[100];
scanf("%s",string1);
printf("original_number = %s\n",string1);
int i;
for(i = 0; i<=strlen(string1); i++){
new_string[i] = string1[i-1];
printf("%c\n",new_string[i]);
}
printf("rev_number = %s\n",new_string);
}
Upvotes: -1
Reputation: 1395
You need to null terminate your string. Add this line at after the for loop where you reverse string.
new_string[i]='\0';
for(i = 0; i<=l; i++){
new_string[i] = string[l-i];
printf("%c\n",new_string[i]);
}
new_string[i]='\0'; // add this
printf("rev_number = %s\n",new_string);
Upvotes: 5