I'm trying to write a code which will replace character in the string user selects with character he/she does. Eg string london if user picks o and a then the output should be landan.
Here is the code:
#include <stdio.h>
#include <string.h>
#define MAXLEN 100
int function2(char str[], char drop, char sub) {
int i = 0; int num = 0;
while (str != NULL) {
if (str[i] == drop) {
str[i] = sub;
num++;
}
i++;
}
return num;
}
int main() {
char d, s;
char my_string[MAXLEN];
printf("Input your string\n");
scanf("%s", &my_string);
printf("Input character you want to drop\n");
scanf(" %c", &d);
printf("Now character you want to substitute\n");
scanf(" %c", &s);
function2(my_string, d, s);
printf("The string is %s\n", my_string);
return EXIT_SUCCESS;
}
It works up until the point where you actually print the altered string. All I get is Segmentation fault (core dumped). Note that code for function was not mine (I found it on some website, so owner of the original code for function2- Thank you in advance). Any help would be appreciated!
First of all, you should avoid using scanf. If you're interested for the reason and alternatives click here.
But back to your problem
while(str != NULL)
is an infinite loop, because the pointer won't become NULL
while(str[i] != '\0')
should do the trick. It checks each time if you've already arrived at the end of the string.
if (str != null){
while(str[i] != '\0'){
if (str[i] == drop){
str[i] = sub;
num++;
}
i++;
}
}
str is a char array, with str != NULL, you check that the array point to a valid memory address.
With while loop and i++, you loop all characters in array, because the string ends with '\0', you need to stop the loop with while(str[i] != '\0').
Your function runs an infinite loop because str never become NULL, but since i is incremented, str[i] will eventually access memory beyond the end of the string and at some point invalid memory causing a Segmentation fault.
Note also that it is not simple to tell scanf() the maximum number of characters to read into my_string. Using fgets() is much safer and allows a whole phrase to be substituted.
Here is a corrected version:
#include <stdio.h>
#include <stdlib.h>
#define MAXLEN 100
int function2(char str[], char drop, char sub) {
int num = 0;
for (int i = 0; str[i] != '\0'; i++) {
if (str[i] == drop) {
str[i] = sub;
num++;
}
}
return num;
}
int main(void) {
char d, s;
char my_string[MAXLEN];
printf("Input your string\n");
if (!fgets(my_string, MAXLEN, stdin))
return EXIT_FAILURE;
printf("Input character you want to drop\n");
if (scanf(" %c", &d) != 1)
return EXIT_FAILURE;
printf("Now character you want to substitute\n");
if (scanf(" %c", &s) != 1)
return EXIT_FAILURE;
function2(my_string, d, s);
printf("The modified string is %s", my_string);
return EXIT_SUCCESS;
}
Related
I am just running a code to find the length of a given string input by the user in C programming language. I used a loop condition to determine the length but statements inside loop executes when the condition is false also. The code I have tried in c is:
#include <stdio.h>
#define ArrayLength 50
int StringLengthCount();
int main() {
printf("Hello, World!\n");
/*Question: Find inserted string's length, without build in function*/
int c=StringLengthCount();
printf("Your inserted string's length is:%d",c);
return 0;
}
int StringLengthCount(){
printf("\n");
printf("Please enter a sentence to find its length of character:");
char array1[ArrayLength];
fgets(array1,ArrayLength,stdin);
printf("Your inserted string is:%s\n",array1);
int i=0;
int count=0;
while(array1[i]!='\0'){
count++;
printf("%d character is %c",count,array1[i]);
printf("\n");
i++;
}
printf("\n");
printf("Your inserted string's total character i.e string length is:%d",count);
}
I am expecting the result 2 for a sample string input "we", but it gives result 3.
The output result in CLion compiler is given below
enter image description here
Can you kindly tell me why it happens?
If by "statements inside loop executes when the condition is false also" you mean that you see an extra character every time you execute remember that also the line feed (LF alias \n) character that you use to enter your string is part of the acquired string.
So even the empty string has one character that is \n or 0x10.
Your check should be something like this:
while (array1[len] != '\0' && array1[len] != '\n' )
And you function, as suggested in the comments, should have a return and could use just one variable like this:
int StringLengthCount() {
printf("\n");
printf("Please enter a sentence to find its length of character:");
char array1[ArrayLength];
fgets(array1, ArrayLength, stdin);
printf("Your inserted string is:%s\n", array1);
int len = 0;
while (array1[len] != '\0' && array1[len] != '\n' ) {
printf("%d character is %c", len + 1, array1[len]);
printf("\n");
len++;
}
printf("\n");
printf("Your inserted string's total character i.e string length is:%d\n\n",
len);
return len;
}
The function fgets will also read the newline character, so you need to change the condition in the while-loop from str[i] != '\0' to str[i] != '\n'. I have also implemented the suggested changes by Devolus.
#include <stdio.h>
#include <stdlib.h>
#define LEN 50
void string_length();
int main(void)
{
string_length();
return EXIT_SUCCESS;
}
void string_length(void)
{
printf("Enter a string: ");
char str[LEN];
fgets(str, LEN - 1, stdin);
printf("Your entered string is: %s\n", str);
int i = 0;
while (str[i] != '\n') {
printf("The %d. character is '%c'.\n", i + 1, str[i]);
++i;
}
printf("\nThe string's length is %d.\n", i);
}
This might be a rookie question, but I need to make sure that the input given by the user is of data type char [%c] or a string [%s].
If it were an integer, I would just do something like this:
int data, x;
do {
printf("Please enter a number: ");
x = scanf(" %d", &data);
getchar();
} while(x!=1);
So I was wondering if there's a similar way to do this, if the input is supposed to be a string or a character. Thanks, Any help would be appreciated!
Avoid to use %c in scanf() because some unexpected character like \r\n will be input.
You can use a char[2] to receive a single character. An \0 will be filled after your string to represent the end of string, so the length of array must be bigger than 1.
An example:
#include <stdio.h>
int main()
{
char data[2];
scanf("%1s", data);
if (data[0] >= 'a' && data[0] <= 'z') // custom your constraint here
{
// legal
printf("legal: %s", data);
}
else
{
// illegal
printf("illegal: %s", data);
}
return 0;
}
While I input b, the data will be "b\0".
part of the answer is if you just want to read only alphabet you can use below.
#include <stdio.h>
#include <ctype.h>
int main()
{
char ch;
do {
printf("enter a char:");
scanf(" %c",&ch);
}while(!isalpha(ch));
printf("%c",ch);
return 0;
}
Update 1:
Just for the completeness and for the FUN part of the programing, have added code here.
This works well (not tested robustly, you can do if you need to) for the single char input or for a string of length 9.
Remember to type the EOF after input is entered in case length of input is < 9.
and read EOF behavior on same line and new line.
#include <stdio.h>
#include <ctype.h>
#define LEN 10
int main()
{
char ch;
char str[LEN] = {0};
int i = 0;
int ret;
printf("enter a char or string(len = 9) and press EOF if len < 9\n");
do {
if(1== (ret = scanf(" %c",&ch)))
{
if(isalpha(ch))
str[i++] = ch;
}
else
printf("scanf:Error (%d)\n", ret);
}while(ret != EOF && ( !isalpha(ch) || i < LEN-1));
str[i] = '\0';
printf("str is %s\n",str);
return 0;
}
I'm trying to program a loop that counts characters until it receives a certain sentinel value. The sentinel value is supposed to be a #, but I've also tried a 0 and a Z and had the same response.
When it compiles, I receive "warning: comparison between pointer and integer" for lines 16 (the line that calls the sentinel.)
If I don't define the sentinel, but instead rely on logical operators in the while statement, then I receive no error, but have an endless loop.
Thanks!
#include <stdio.h>
int main()
{
#define SENTINEL '#'
char ch;
int chcount;
printf("Enter your text, terminate with a #:");
scanf("%s", &ch);
chcount = 0;
while (ch != SENTINEL)
{
if ((ch >= 'A') && (ch <= 'Z'))
{
chcount = chcount +1;
printf("You have entered %d characters", chcount);
}
}
return(0)
}
With the %s format specifier, scanf expects the address of a char buffer, where the string you type will be copied.
And you gave the address &ch of a single char, which is obviously not enough to contain a "word" from input with its terminating null character.
Moreover, your loop reads no input from the user. Thus the endless loop.
This is because the way you use scanf(), with %s format specifier you are writing to a char*, not the char ch (as you've declared). In order to write to a single char variable, you should use a %c format specifier.
To fix this you should either use f.e. getchar() instead of scanf() or use scanf() (and change ch to char* then) but iterate over scanned string to check whether there is #.
I would recommend the first solution.
The while loop never ends so I changed your while loop.
I tried to change your program to:
#include <stdio.h>
#define SENTINEL '#'
int main()
{
char ch;
int chcount;
printf("Enter your text, terminate with a #:");
chcount = 0;
while ((ch = getchar()) != SENTINEL)
{
if ((ch >= 'A') && (ch <= 'Z'))
{
chcount = chcount + 1;
printf("You have entered %d characters\n", chcount);
}
}
return(0);
}
Some issues I found with your code:
scanf("%s", &ch);
It should be
scanf("%c", &ch);
Next, semicolon missing here: return(0);
However, since your aim is:
I'm trying to program a loop that counts characters until it receives a certain sentinel value. The sentinel value is supposed to be a #
I suggest moving your scanf() inside while loop:
#include <stdio.h>
int main()
{
#define SENTINEL '#'
char ch='0';
int chcount;
printf("Enter your text, terminate with a #:");
chcount = 0;
int i=0;
while (ch != SENTINEL)
{ scanf("%c", &ch);
if ((ch >= 'A') && (ch <= 'Z'))
{
chcount = chcount +1;
printf("You have entered %d characters", chcount);
i++;
}
}
return(0);
}
here is a working version of the posted code.
It contains numerous corrections.
Corrections include consistent/usable indentation and logic corrections
Note: not all implementations have the getline() function
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
int main( void )
{
int sentinelFound = 0;
#define SENTINEL '#'
char* line = NULL;
size_t lineLen = 0;
printf("Enter your text, terminate with a #:");
int chcount;
getline(&line, &lineLen, stdin );
size_t i;
for( i=0; i<lineLen; i++)
{
if( SENTINEL == line[i] )
{
sentinelFound = 1;
break;
}
if ((line[i] >= 'A') && (line[i] <= 'Z')) // only count capital letters
{
chcount = chcount +1;
}
}
free( line );
if( !sentinelFound )
printf( "You did not enter the sentinel character!" );
else
printf("You have entered %d capital characters\n", chcount);
return(0);
} // end function: main
I want to run this code but i can not. i have yet started to learn pointer in C. I am trying to get the addresses of letters. Problem is printf("in adress: %p\n",p[i]);` Thanks
#include <stdio.h>
int main(void){
char letter;
int c=0;
int i;
char pattern[7];
char *p;
printf("Enter a letter: ");
scanf("%c",&letter);
printf("Enter a pattern: ");
scanf("%s",pattern);
p=pattern;
for(i=0;i<7;i++)
{
if(letter==pattern[i])
{
c++;
printf("Letter < %c > is found in pattern %s\n",letter,pattern);
printf("in adress: %p\n",p[i]);
printf("index:%d\n",i);
}
}
if(c==0)
printf("The pattern does not include any letter");
return 0;
}
This line of code has some potential problem that can easily happen
scanf("%s",&pattern);
you can make it a little bit safer like this
scanf("%6s",&pattern);
it's a 6 because you need one extra byte '\0' at the end of the string, which takes us to the next problem
for(i=0;i<7;i++)
here you are assuming that all bytes are non-nul which would be ok except that you are reading a string with scanf() and unless you create the array one byte bigger than the number of characters you want to store in it, scanf() will overflow the array i.e. write a byte beyond it's bounds.
Adding the "%6s" length specifier to the format string solves this, but you can only store 6 non-nul bytes in the array, and for the other problem
for (i = 0 ; pattern[i] != '\0' ; i++)
would be better, because you don't need to know the length of the string in advance and you don't risk reading past the end of the array.
Try this:
#include <stdio.h>
int main(void)
{
char letter;
char c;
int i;
char pattern[7];
printf("Enter a letter: ");
if (scanf(" %c",&letter) != 1)
{
printf("error: invalid input.\n");
return -1;
}
printf("Enter a pattern: ");
if (fgets(pattern, sizeof(pattern), stdin) == NULL)
{
printf("error: end of file.\n");
return -1;
}
for (i = 0 ; pattern[i] != 0 ; ++i)
{
if (letter == pattern[i])
{
c++;
printf("Letter < %c > is found in pattern %s\n", letter, pattern);
printf("in adress: %p\n", pattern + i);
printf("index :%d\n", i);
}
}
if (c == 0)
printf("The pattern does not include any letter");
return 0;
}
You also was printg the address wrong, because in pointer[i] the subscript operator dereferences the pointer, it's equivalent to *(poitner + i).
Why in this code its doing an infinite loop without prompting again the user
#include <stdio.h>
#include <string.h>
#include <ctype.h>
void to_rot13(char* value);
int main(){
char word[1024];
printf("C ROT13\nSome data to code or decode\n");
while (1){
printf(": ");
scanf("%[^\n]s", word);
to_rot13(&word);
printf(": %s\n", word);
}
return 0;
}
void to_rot13(char* value){
unsigned int x;
for (x = 0; value[x] != '\0'; x++){
if ((value[x] < 'A') || (value[x] > 'Z' && value[x] < 'a') || (value[x] > 'z')){}
else if (tolower(value[x]) <= 'm'){value[x] = value[x] + 13;}
else{value[x] = value[x] - 13;}
}
}
I would like to prompt again the user
I can't be more precice.
scanf("%[^\n]s", word);
leaves the newline in the input buffer, so the next scanf immediately returns without reading any further input, since the first char left in the buffer is a newline. You need to remove the newline from the input buffer.
int c;
do {
c = getchar();
}while(c != '\n' && c != EOF);
if (c == EOF) {
exit(EXIT_FAILURE); // input borked
}
Also, heed the compiler's warning and pass word instead of &word to to_rot13.
You're sending &word to to_rot13 (you should be sending word or &word[0])