Replace character - c

I've done this code so far but I don't get a final output (blank).
Output that I expected is when I put a string Hello World and replace o with i the string will be Helli Wirld. But I got nothing in the final output.
char * substitute(char *, char, char);
int main(void){
char arr[255];
char i,j;
printf("Enter a string: ");
gets(arr);
printf("Find a char: ");
scanf(" %c", &i);
printf("Replace with: ");
scanf(" %c", &j);
printf("Final output: ");
printf("%s", substitute(arr, i, j));
return 0;
}
char * substitute(char *data, char find, char replace){
while(*data!='\0'){
if(*data==find){
*data=replace;
}
data++;
}
return data;
}

In substitute() you return the pointer data that you have incremented in your while loop, so now it points to the terminating '\0' and that's what you printf().
You could either use a separate local variable for traversing the string, or you don't use the return value of substitute() at all and replace
printf("%s", substitute(arr, i, j));
by
substitute(arr, i, j);
printf("%s", arr );

Related

Function is telling me the length of the string and not the number of occurrences of the char inputted by the user

#include <stdio.h>
#include <string.h>
int countLetters(char *string1, char letter){
int count=0;
for(int i=0; i<strlen(string1); i++){
if(string1[i]=letter){
count++;
}
}
return count;
}
int main(){
char string1[200];
char letter;
int count;
printf("\nEnter the string: \n");
fgets(string1, 200, stdin);
printf("\nEnter character to be searched: \n");
scanf("%c", &letter);
count = countLetters(string1, letter);
printf("\nThe number of occurrences: %d", count);
}
I was expecting for the function to output the number of times each letter of the array was equal to the char(letter) inputted by the user, but it is just giving me the length of the string.
Change the line:
if(string1[i]=letter){
to
if(string1[i]==letter){
Note, that the string1[i]=letter was overwriting data in string1[i].
You have to use equal equal to operator instead of assignment operator in if condition like this
if(string1==latter)
in your if condition if(string1=latter) value of latter variable is assign to string1[i]

Is there some problem with the following c code

When i compile and run this program my input string is not same as output.
#include<stdio.h>
int main()
{
int n; char ch[100];
scanf("%d : %5s", &n, ch);
printf("%d : %s", n, ch); // there is some problem with output of the string
return 0;
}
input:
45
asdf
output:
45 : ∟sëuⁿ■a
The scanf part will read until it hits the colon, but it will not read the colon itself, which means the following integer will not be read correctly and the rest of the string will be parsed incorrectly (if at all).
Try removing the colon (:) from the scanf
#include <stdio.h>
int main()
{
int n; char ch[100];
scanf("%d %5s", &n, ch);
printf("%d : %s", n, ch);
return 0;
}
get rid of the ":" in scanf(), nothing is read after the "%d" input

Why am I getting garbage value here?

Its a program to convert integer stored in a string into an int. Please help.
#include<stdio.h>
#include<math.h>
int main()
{
int num, n, i;
num = 0;
printf("Enter n\n");
scanf("%d", &n);
char string[n+1];
printf("Enter the number\n");
scanf("%s", &string);
for(i=0;string[i]!='\0';i++)
{
num = num + string[i]*pow(10,n-i-1);
}
printf("The required number is %d", num);
return 0;
}
In typical environment, character codes for digits are not equal to the numbers the digits represent for.
Character codes for digits in C are defined to be continuous, so you can convert the character codes to the corresponding numbers by subtracting '0' from the character code.
num = num + (string[i]-'0')*pow(10,n-i-1);
By the way, there are some better ways to do the conversion:
sscanf(string, "%d", &num); /* available in stdio.h */
num = atoi(string); /* stdlib.h is required for atoi() */
Also note that scanf("%s", &string); invokes undefined behavior because a pointer to array is passed where char* (a pointer to char) is required. The & should be removed.
In your code the size of your string must be equal to n+1 ,so you can removed all this
printf("Enter n\n");
scanf("%d", &n);
char string[n+1];
for(int i=strlen(string)-1;i>=0;i--)
I started from the last caracter in my string to the first caracter and I multiplied each caracter by j (j=1 then j=10 then j=100 .....)
#include<stdio.h>
#include<math.h>
int main()
{
int j=1;
char string[100];
printf("Enter the number\n");
scanf(" %s", string);
int num=0;
for(int i=strlen(string)-1;i>=0;i--)
{
int k=string[i]-'0';//subtract each case by '0'
num=num+k*j;
j=j*10;
}
printf("The required number is %d", num);
return 0;
}

convert a char element from an array to an int

Hi I'm trying to convert elements of an array into integers using input from the user.
#include <stdio.h>
#include <string.h>
int main()
{
char i[9]={'-','-','-','-','-','-','-','-','\0'};
int j;
printf ("enter an integer for an element ");
sscanf(i, "%d", &j);
return 0;
}
I read somewhere that using sscanf is one way to do it but I don't know the correct way to use it.
scanf reads from the standard input.
sscanf reads from a string.
Example of scanf usage:
#include <stdio.h>
int main()
{
char str1[20];
printf("Enter name: ");
scanf("%s", str1);
printf("First char converted to int: %d\n", str1[0]);
int j = str1[1];
printf("Second char converted to int: %d\n", j);
return(0);
}
Input and Output:
Enter name: marcel
First char converted to int: 109
Second char converted to int: 97
As Marcel said, there are two different functions that do different things.
You request convert elements of an array into integers using input from the user, so I can think you want this:
char i[9]={'-','-','-','-','-','-','-','-','\0'};
int j;
printf ("enter an integer for an element ");
scanf("%i", &j);
printf("Element[ %i ] in integer is: %i", j, i[j]);

Can't I only print one loop? - C

I want to print three char variables with a for loop but I don't know why only last is printed.
This is my code:
int main (){
int i, j, k;
char word[50], old[1], new[1];
printf("Enter a word: ");
gets(word);
printf("Enter desired letter to substitute: ");
gets(old);
printf("Enter the new letter: ");
gets (new);
for (i = 0; i<strlen(word); i++){
printf("%c", word[i]);
}
for (j = 0; j<strlen(old); j++){
printf("%c", old[j]);
}
for (k = 0; k<strlen(new); k++){
printf("%c", new[k]);
}
}
I don't get why this simply code doesn't work.
Your code overflows the buffers for old and new because they do not contain enough space for a 1 character string. In C strings needs to be null terminated, so a char array must always be one element longer than the maximum number of characters it must contain. For example your word array can only hold a 49 character string, since the 50th element must be the null.
So old and new must both be char[2] arrays.
Your loops are unnecessary and incorrect. You can print the values directly without specifying the index. In your post you're trying to loop through each index and print a character at a time. Here's what you should do:
int main (){
int i, j, k;
char word[50], old[2], new[2];
printf("Enter a word: ");
gets(word);
printf("Enter desired letter to substitute: ");
gets(old);
printf("Enter the new letter: ");
gets (new);
printf("%s\n", word);
printf("%s\n", old);
printf("%s\n", new);
}

Resources