Can you help me in my code? I'm trying to concatenate characters using (+) sign but it doesn't work what shall I do? and my homework tells me to do it in that way with only using stdio.h library. that's what I wrote till now and thanks. This is the question-> enter image description here
#include <stdio.h>
char reverse(char string[], int stringLength){
static int i;
i=stringLength;
if(stringLength!=0){
if(i>=0){
return string[i]+reverse(string[i-1],i--);
}
}
return string;
}
int main(void) {
int stringLength=0;
char string[100];
printf("Enter a string that will be reversed: ");
scanf("%s",string);
for (stringLength=0; string[stringLength++]!='\0';){};
printf("%d", stringLength);
reverse(string, stringLength);
printf("the reverse is :%s:\n", string);
return 0;
}
Related
got this little problem, I made this code for my task, it should input strings and print it in revese, the loop should end when you enter end, but it doesnt end, I know this is not how you check strings but I don't know how to correct it. Thanks in advance for help.
#include <stdio.h>
#include <stdlib.h>
void reverse(char str[]){
int length;
for(length=strlen(str)-1; length >= 0; length--){
printf("%c",str[length]);
}
}
int main(void){
char str[]="";
while(str != "end"){
printf("\nEnter string: ");
scanf("%s", str);
reverse(str);
}
return 0;
}
you have many problems in your code :
when you write char str[]=""; this is will create a string of size = 1 only which will not accept any string you enter except for only one char , so you should do char str[50]; where 50 is the max expected length of the entered string.
it's not while(str != "end") it's , while(strcmp(str,"end") != 0) as you want to compare the strings itself not addresses
it's better to write scanf("%49s", str); than scanf("%s", str); just to make sure that the entered string will always fit in your array
in this line length = strlen(str)-1; , the strlen function return unsigned long long , so you should typecast that and write length = (int)strlen(str)-1; instead
with this all being said , this is the edited code :
#include <stdio.h>
#include <string.h>
void reverse(char str[]){
int length;
for(length = (int)strlen(str)-1; length >= 0; length--){
printf("%c",str[length]);
}
}
int main(void){
char str[50];
while(strcmp(str,"end") != 0){
printf("\nEnter string: ");
scanf("%49s", str);
reverse(str);
}
return 0;
}
and this is the output:
Enter string:abcd
dcba
Enter string:end
dne
Process finished with exit code 0
I'm trying to run this program by taking input from the user and I'm getting a Segmentation fault (core dumped) error. Can anyone help me to find out why this error is occurring and how can I fix this error? My code is given below:
// C program to remove the occurrences of a specific char from the given string.
#include <stdio.h>
char *squeeze(char *a[], int b){
int i,j;
for(i=j=0; *(*a+i)='\0';i++){
if(*(*a+i)!=b)
*(*a+j++)=*(*a+i);
}
*(*a+j)='\0';
return *a;
}
void main(){
char *c[1];
char d[2];
int e;
printf("Enter the string: ");
scanf("%s",*c);
printf("Enter the character to delete: ");
scanf("%c",d);
e=d[0];
printf("Resulting string is:%s.\n",squeeze(c,e));
}
You have a few problems in your code. First, as pointed out in the comments, your declaration of c as an array of char * length one is wrong. Declare it as an array of char or some max length. The will make the squeeze code simpler, with less dereferences. Next your for loop inside of squeeze is wrong, you have an assignment statement there in the middle, when you really want to check for inequality. Your second scanf needs a space in it to get rid of the white space character leftover in the input buffer. So, making these changes, your code should look something like this:
#include <stdio.h>
char *squeeze(char *a, int b){
int i,j;
for(i=j=0; *(a+i) != '\0';i++){
if(*(a+i)!=b)
*(a+j++)=*(a+i);
}
*(a+j)='\0';
return a;
}
int main() {
char c[100] = {0};
char d[2];
int e;
printf("Enter the string: ");
scanf("%99s",c);
printf("Enter the character to delete: ");
scanf(" %c",d);
e=d[0];
printf("Resulting string is: %s.\n",squeeze(c,e));
}
EDIT: Specify string length with scanf when reading in c.
// C program to remove the occurrences of a specific char from the given string.
#include<stdio.h>
char * squeeze(char a[], char b){
for(i=0; a[i]!='\0';i++){
if(a[i]==b){
a[i]=a[i+1];
while(a[i++]!='\0'){
a[i]=a[i+1];
}
}
}
return a;
}
int main(){
char c[50];
char d;
printf("Enter the string:");
scanf("%s",c);
printf("Enter the character to delete:");//add \n
scanf(" %c",&d);
printf("Resulting string is:%s\n",squeeze(c,d));
return 0;
}
My code should delete all vowels from the string that i give. But it does not delete if the vowel is the last character of the string.
#include <stdio.h>
#include <string.h>
int main()
{
int i=0;
char str[100];
printf("Enter a string :\n");
gets(str);
int len=strlen(str);
while(str[i]!='\0')
{
printf("%c",str[i]);
if(str[i]=='A'||str[i]=='a'||str[i]=='E'||str[i]=='e'||str[i]=='I'||str[i]=='i'||str[i]=='O'||str[i]=='o'||str[i]=='U'||str[i]=='u')
printf("\b");
i++;
}
return 0;
}
Like if i provide the string Hello it prints Hllo where it should print Hll ...But if i change the while condition to (i
I guess printing \b doesn’t do what you think it does. It does not delete the last printed character, it just prints an additional ‘backspace’ character, which on some output devices (such as console) moves backwards by one character. (Then, the next character overwrites the one you wanted ‘deleted’.)
Don’t do that. Instead, move the ‘if’ statement so that you don’t print those vowels in the first place!
You can try this
#include <stdio.h>
#include <string.h>
int main()
{
int i=0;
char str[100];
printf("Enter a string :\n");
fgets(str,100, stdin);
int len=strlen(str);
while(str[i]!='\0')
{
if(str[i]=='A'||str[i]=='a'||str[i]=='E'||str[i]=='e'||str[i]=='I'||str[i]=='i'||str[i]=='O'||str[i]=='o'||str[i]=='U'||str[i]=='u'){
continue;
}else{
printf("%c",str[i]);
}
i++;
}
return 0;
}
I want the string to be printed till character ('e') comes.
Code which I tried:-
#include <stdio.h>
int main() {
int a,i,x;
char b[10];
char ch;
//enter input string
for(i=0;i<10;i++)
scanf("%c",&b[i]);
for(i=0;i<10;i++)
if(b[i]!='e')
printf("%c",b[i]);
return 0;
}
Input:abcdefghij
Actual output:abcdfghij
Desired output:abcd
Question : Where am I wrong ? Will putting a break inside if block work here?
This is much cleaner if you want to use scanf.
#include <stdio.h>
int main()
{
char b[101];
scanf("%100s", b);
printf("%s\n", b);
return(0);
}
Or even better.
#include <stdio.h>
#define MAX_LENGTH 100
int main()
{
char b[MAX_LENGTH+1]; // add 1 for the terminating zero
scanf("%100s", b);
printf("%s\n", b);
return(0);
}
This one uses fgets to read the entire line.
#include <stdio.h>
#define MAX_LENGTH 100
int main()
{
char b[MAX_LENGTH];
fgets(b, MAX_LENGTH, stdin);
printf("%s", b);
return(0);
}
How to print a string till limit?
What code should do is use fgets().
Avoid using scanf(). Is is too easy to use wrong.
#include <stdio.h>
#include <string.h>
int main() {
char b[100];
if (fgets(b, sizeof b, stdin)) {
// If code needs to lop off the potential \n at the end
b[strcspn(b, "\n")] = '\0';
printf("%s\n", b);
}
return 0;
}
Advanced issues include how to handle excessively long input lines and error handling - not shown here.
Here is what you need to do
#include <stdio.h>
int main()
{
int a,i,x;
char b[10];
char ch;
//enter input string
for(i=0;i<10;i++)
{
scanf("%c",&b[i]);
}
for(i=0;i<10;i++)
{
if(b[i]=='e')
{
break;
}
}
return 0;
}
re
There are several mistakes!
If you are initializing your loops from 0 then you need to set the condition till i<100.
Change your format specifiers to %s.
Change your IF statement to if(b[i]!='\0').
#include <stdio.h>
int main()
{
int i;
char b[10];
for(i=0;i<10;i++)
{
scanf("%c",&b[i]);
}
for(i=0;i<10;i++)
{
if(b[i]=='e')
{
break;
}
printf("%c",b[i]);
}
return 0;
}
Here is my code:
#include<stdio.h>
#include<string.h>
int string_length(char s[]);
main()
{
char s[50];
int l;
printf("Enter the string\n");
scanf("%s\n",s);
l=strlen(s);
printf("Length of string = %d\n",l);
}
int string_length(char s[])
{
int i;
i=0;
while(s[i] != '\0')
++i;
return i;
}
After compile it scan for two input values.
What's wrong with my code?
Get rid of the newline in the scanf.
scanf("%s",s);
That should get this code to work.
But I am unable to understand why you wrote a function to compute string length if you had to use strlen().
HTH,
Sriram.
You're calling strlen instead of your own string_length in main.
const size_t sillyStrlen(const char* text) {
if (*text) {
return sillyStrlen(text + 1) + 1;
}
return 0;
}
Buy a book, see The Definitive C Book Guide and List
Read about the language syntax
Learn how to use pointers
You've just learned how to write a int strlen(char[]) function.
Try This one.
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
char *str;
printf("Enter a string\n");
gets(str);
printf("The size of the string is %d",strlen(str));
getch();
return 0;
}