Integer from pointer warning - c

I have I problem. I get 2 warnings from console, but I dont know what's wrong with my code. Can you have look?
Program suppose to show lines with at least 11 characters and 4 numbers
#include <stdio.h>
#include <ctype.h>
int main()
{
char line[200];
printf("Enter a string: \n");
while(fgets(line, sizeof(line),stdin))
{
int numberAlpha = 0;
int numberDigit = 0;
if(isalpha(line)) numberAlpha++;
else if(isdigit(line)) numberDigit++;
if(numberAlpha+numberDigit>10 && numberDigit>3) printf("%s \n", line);
}
return 0;
}

Both isalpha() and isdigit() takes an int, not a char *, as argument.
In your code, by passing the array name as the argument, you're essentially passing a char * (array name decays to the pointer to the first element when used as function argument), so, you're getting the warning.
You need to loop over the individual elements of line and pass them to the functions.
That said, just a suggestion, for hosted environment, int main() should be int main(void) to conform to the standard.

isalpha and isdigit are supposed to test if a char taken as int (a char can be safely converted to an int) is the encoding of an alphanumeric or digit character. You pass a char array, not an individual char. You need to test each char of the string you got, so you need a loop as:
for (int i=0; i<strlen(line); i++) {
if (isalpha(line[i])) numberAlpha++;
...
}
It is better to compute the length once:
int length = strlen(line);
for (int i=0; i<length; i++) {
...
}
You may also use a pointer to move along the string:
for (char *ptr = line; *ptr!=`\0`; ptr++) {
if (isalpha(*ptr)) ...
...
}

isalpha() and isdigit() functions take an int. But you are passing a char* i.e. the array line gets converted into a pointer to its first element (see: What is array decaying?). That's what the compiler complains about. You need to loop over line to find the number of digits and alphabets in it.
Also note that fgets() will read in the newline character if line has space. So, you need to trim it out before counting.
#include <stdio.h>
#include <ctype.h>
#include <string.h>
int main(void)
{
char line[200];
printf("Enter a string: \n");
while(fgets(line, sizeof(line),stdin))
{
int numberAlpha = 0;
int numberDigit = 0;
line[strcspn(line, "\n")] = 0; // Remove the trailing newline, if any.
for (size_t i = 0; line[i]; i++) {
if(isalpha((unsigned char)line[i])) numberAlpha++;
else if((unsigned char)isdigit(line[i])) numberDigit++;
}
printf("alpha: %d, digits:%d \n", numberAlpha, numberDigit);
}
return 0;
}

Ok, i got something like this:
#include <stdio.h>
#include <ctype.h>
int main()
{
char line[200];
printf("Enter a string: \n");
while(fgets(line, sizeof(line),stdin))
{
int numberAlpha = 0;
int numberDigit = 0;
int i;
for(i=0; i<strlen(line); i++){
if(isalpha(line[i])) numberAlpha++;
else if(isdigit(line[i])) numberDigit++;
}
if(numberAlpha+numberDigit>10 && numberDigit>3) printf("%s \n", line);
}
return 0;
}
Now the question is, if it is passible to make it first accepts data and then display only those line which follows the if statment. Now it shows line just after input it.

Related

Problem with finding the length of a string using pointers in a function in c

I got a task from my college which is to find the length of a string using pointers in a function.Here is my code:
#include <stdio.h>
#include <stdlib.h>
#define max 100
int lengthchar(char *array1[])
{
int a, x = 0;
for (a = 0; *(array1++) != '\0'; a++)
{
x++;
}
return x;
}
int main()
{
char arr1[max];
int length;
printf("Enter string\n");
gets(arr1);
length = lengthchar(arr1);
printf("Length=%d", length);
return 0;
}
But when I give input something bellow:
Enter string: av
Length=9
It shows the wrong length, what's my mistake?
This is because of the way to pass a string pointer as an argument to the function.
Just change char *array1[] to char *array1 or array1[].
Have a look at the implementation below:
int lengthchar(char array1[])
{
int a,x=0;
for(a=0;*(array1++)!='\0';a++)
{
x++;
}
return x;
}
PS: variable a can be removed by using a while loop.
In your function signature, you are telling the compiler that lenghtchar() expects a pointer to character strings, or **char in other words.
What you really want to do is to change your function from int lengthchar(char *array1[]) to int lengthchar(char array1[]) or int lengthchar(char *array1). This is a bit tricky since in C, you can address an array by using the address of its first element (aka, by using pointer to its first item).
Expert C Programming has a dedicated chapter on this topic.
Now, coming to your lengthchar() function, I would do some refactoring to eliminate the variable a and use a while loop instead. I have also included another alternative implementation that relies on pointer arithmetic (more fun to me :) )
Note also that I used fgets() instead of gets() which is considered deprecated since it does not do any bounds checking.
#include <stdio.h>
#include <stdlib.h>
#define max 100
/*
* returns the lenght of the string excluding the terminating
* NULL character
*/
int lengthchar(char *array1) {
int x = 0;
while (*array1++)
x++;
return x-1;
}
int lengthchar1(char *array1){
char *p;
for (p = array1; *p; p++)
;
return p - array1 - 1; /* -1 for \0 */
}
int main() {
char arr1[max];
int length;
printf("Enter string\n");
fgets(arr1, max, stdin);
length = lengthchar(arr1);
printf("Length=%d", length);
return 0;
}
On declaring or defining a function that shall take an array of type T as an argument, use the notation: T *array or T array[]; T may stand for any valid C data type such as char, int, float, double, etc...
As for your loop, the variable a seems to be redundant because it has no effect on any part of the program. The return value of the function lengthchar() is one more than the number of characters inputted.
Also you should avoid the function gets() because it is deemed dangerous and has been removed from the C standard; use fgets() instead.
Your code should look something like this:
#include <stdio.h>
#include <string.h>
#define max 100
int lengthchar(char *str)
{
int len = 0;
while (*str++)
{
len++;
}
return len;
}
int main()
{
char str1[max];
printf("Enter string:");
fgets(str1, max, stdin);
str1[strcspn(str1, "\n")] = 0; // remove new-line character
int length = lengthchar(str1);
printf("Length = %d\n", length);
return 0;
}

why the atoi function is not working?

The program stops working.
Even if I put only one int.
I tried many different ways but can't figure out what is wrong.
I am trying to take input of integers separated by space.
There can be any no of integers.
#include<stdio.h>
#include<stdlib.h>
int main(void) {
int i,j=0;
int b[100];
char a[100];
fgets(a,100,stdin);
for(i=0;i<strlen(a);i++)
{
b[i] = atoi(a[j]);
j=j+2;
}
for(i=0;i<strlen(b);i++)
{
printf("%d ",b[i]);
}
}
Here is the prototype of atoi you have to use a character array but you are sending a character only.atoi(str[i])
int atoi(const char *str)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
int val;
char str[20];
strcpy(str, "98993489");
val = atoi(str);
printf("String value = %s, Int value = %d\n", str, val);
return(0);
}
Do the following:
for(i = 0; i < strlen(a); i += 2)
{
b[j] = a[i];
j++;
}
though atoi() accepts only a string argument or u can say constant char pointer and str[i] is nothing but a pointer pointing at a single character of the character array.
Therefore once we pass atoi(str[i]) , that means we are passing a character to the function, which will give an error.
Therefore you must pass the address of that particular character from where you want to convert the substring into a number i.e. atoi(&str[i]).

Assignment makes pointer from integer without a cast and vice versa in c

I'm trying to write a program that gets a string, and a number, and calculates the length of it and shifting all the elents right.
I have 2 errors:
1.assignment makes pointer from integer without a cast.
2.assignment makes integer from pointer without a cast.
#include <stdio.h>
#include <string.h>
#define N 10
int myStrlen(char*);
void shiftRight(char*, int);
int main() {
char str[N] = {0};
int num = 0;
int len;
/* input of the string */
scanf("%s",str);
scanf("%d",&num);
len=myStrlen(str);
if(num>=0) {
shiftRight(str, num);
printf("%s\n",str);
}
else
{
printf("%s\n", str);
}
return 0;
}
int myStrlen(char*str)
{
int my_len=0;
while (str[my_len] != '\0')
{
my_len++;
}
return my_len;
}
void shiftRight(char* str, int num)
{
int i;
char* j;
int count;
j=(str[N-1]);
for(count=0;count<num;count++)
{
for(i=N-1;i>0;--i)
{
str[i]=str[i-1];
}
str[0]=j;
}
}
Your answers are welcome,anf if you anything wrong with this code,please mention it.
As your compiler will have told you, pointer from integer without a cast is at
j=(str[N-1]);
And integer from pointer is at
str[0]=j;
You should have declared j as char j;
But now when i run it, and typing lets say ball as a string and 1 to
be a number, i get nothing from the program instead of getting "lbal"
You have all the correct elements but that's not enough. Writing a program is telling a story, you need to set the scene, describe what happens along the way and conclude your narrative. A story with elements out of order is nonsense, as is a program.
Specific issues with your code: you're saving of the last character (to restore it to the beginning of the string) is in the wrong place; you're using the allocation of the string when you should be using it's length (and conveniently, you have a function for that!); this is really more of a rotation than a shift; use the most descriptive variable names you can, not the shortest you can get away with; pick one indentation style and stick with it -- it can change between programs you write but shouldn't change within an individual program.
Below is a rework of your code addressing some of the issues above:
#include <stdio.h>
#define STRING_SIZE 10
int myStrlen(char *string)
{
int length = 0;
while (string[length] != '\0')
{
length++;
}
return length;
}
void rotateRight(char *string, int number)
{
int length = myStrlen(string);
for (int count = 0; count < number; count++)
{
char j = string[length - 1];
for (int i = length - 1; i > 0; i--)
{
string[i] = string[i - 1];
}
string[0] = j;
}
}
int main()
{
char string[STRING_SIZE] = {0};
int number = 0;
/* input of the string */
scanf("%s", string);
scanf("%d", &number);
if (number > 0)
{
rotateRight(string, number);
printf("%s\n", string);
}
else
{
printf("%s\n", string);
}
return 0;
}
OUTPUT
% ./a.out
elephant
3
anteleph
%

Splitting string using symbols in C

I have written a code trying to split a long string to get simpler strings so that i could sort them out... When i break from the nested loop, does it break up to the first loop entirely??
My input is "&$(, My,na$me(is"
the output that i wanted is "My na me is"
How can i solve this??
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main(){
char splitter[100];
char mystring[1000];
char newstring[1000][1000];
int i,j,z,k=0;
scanf("%s", splitter);
scanf("%s", mystring);
for (i=0; i<1000; i++){
for (j=k; j<1000; j++){
for (z=0; z<100; z++){
if (mystring[j]==splitter[z]){
k++;
break;
}
else
{
newstring[i][j]=mystring[j];
}
}
if (mystring[j]==splitter[z])
break;
}
}
for (i=0; i<10; i++){
printf("%s ", newstring[i]);
}
return 0;
}
First; C is not Python, you can't just use indent to denote blocks, you must use braces, i.e. { and }.
Second, no a break only breaks the closest-most loop its in, there's no way to break out of more than one level.
Third, you're looping over the strings as if they're always 100 characters long which they won't always be (for instance in your example they're not). This is wrong, you should use strlen() to figure out how long they are.
Fourth, you should check the return values of your scanf() calls, since it can fail.
Fifth, newstring is declared as an array of arrays, i.e. a gigantic one-megabyte 2D "square" of characters, which is clearly not how you're using it.
#include <stdio.h>
#include <string.h>
int main(){
char splitter[100];
char mystring[1000];
char *tokens[500];
char *token;
int i=0;
scanf("%99[^\n]%*c", splitter);
scanf("%999[^\n]", mystring);
token = strtok(mystring, splitter);
while(token){
if(i)
putchar(' ');
printf("%s", token);
tokens[i++] = token;
token = strtok(NULL, splitter);
}
putchar('\n');
return 0;
}
Though there are many mistakes in your code and I am unable to find out why are you doing those mistakes.For example I dint get why are you scaning two arrays and why are you using a two-D array.Another thing I would like to tell you is that scanf doesnt work when there is spaces in the string.According to your problem here is very simple solution.
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main(){
char splitter[100];
char mystring[1000];
char newstring[1000][1000];
int i,j,z,k=0;
gets(splitter);
for(i=0;i<strlen(splitter);++i){
if(((splitter[i])>=65 &&(splitter[i]<=90)) || ((splitter[i]>=97)&&(splitter[i] <=122)))
mystring[k++]=splitter[i];
else
mystring[k++]=' ';
}
printf("%s\n",mystring);
//scanf("%s", mystring);
return 0;
}
Check the below code:
int main()
{
int i=0,j=0,t,f=0;
char s[20];
char b[20];
printf("Enter the string\n");
scanf("%s",&s);
while(s[i] != '\0')
{
t = s[i];
if((t>=65 && t<=90) || (t>= 97 && t<=122))
{
b[j++] = s[i];
f = 1;
}
else
{
if(f)
b[j++] = ' ';
}
i++;
}
b[j] = '\0';
printf("%s\n",b);
return 0;
}

Problems with simple c task

So after a few years of inactivity after studying at uni, I'm trying to build up my c experience with a simple string reverser.
here is my code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*
*
*/
int main(int argc, char** argv) {
reverser();
return(0);
}
int reverser(){
printf("Please enter a String: ");
//return (0);
int len;
char input[10];
scanf("%s",&input);
int quit = strcmp(input,"quit");
if(quit == 0){
printf("%s\n","Program quitting");
return(0);
}
len = strlen(input);
printf("%i\n",len);
char reversed[len];
int count = 0;
while (count <= (len-1)){
//printf("%i\n",(len-count));
reversed[count] = input[(len-1)-count];
count++;
}
//printf("%s\n",input);
printf(reversed);
printf("\n");
reverser();
}
When I input "hello", you would expect "olleh" as the response, but I get "olleh:$a ca&#",
How do I just get the string input reversed and returned?
Bombalur
Add a '\0' at the end of the array. (as in, copy only chars until you reach '\0' - which is the point at array[strlen(array)], then when you're done, add a '\0' at the next character)
Strings are conventionally terminated by a zero byte. So it should be
char reversed[len+1];
And you should clear the last byte
reversed[len] = (char)0;
you forgot the \0 at the end of the string
This is because you are creating an array with size 10. When you take in some data into it (using scanf) and the array is not filled up completely, the printf from this array will give junk values in the memory. You should iterate for the length of the input by checking \n.
must have a size + 1 to string length so that you can have a \0 at the end of string that will solve your problem
The following is a (simple and minimal implementation of) string reverse program (obviously, error conditions, corner cases, blank spaces, wider character sets, etc has not been considered).
#include <stdio.h>
int strlen(char *s)
{
char *p = s;
while (*p)
p++;
return p - s;
}
char * strrev(char a[])
{
int i, j;
char temp;
for (i=0, j=strlen(a)-1 ; i<j ; i++, j--) {
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
return a;
}
int main()
{
char str[100];
printf("Enter string: ");
scanf("%s", str);
printf("The reverse is %s \n", strrev(str));
return 0;
}
Hope this helps!

Resources