Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,870 questions

51,793 answers

573 users

How to check if string is empty in C

4 Answers

0 votes
#include <stdio.h>
#include <string.h>

#define SIZE 16

int StringIsEmpty(char s[], int size);

int main(void)
{
	char s[SIZE] = "That Code!";
    
    if (StringIsEmpty(s, SIZE))
        printf("Empty\n");
    else
        printf("NOT Empty\n");
    
    memset(s, '\0', sizeof(s));
    if (StringIsEmpty(s, SIZE))
        printf("Empty\n");
    else
        printf("NOT Empty\n");

    return 0;
}

int StringIsEmpty(char s[], int size)
{
    int i = 0, count = 0;
    
    while (s[i++] == '\0') count++;
    
    return size == count;
}
 
/*
run:

NOT Empty
Empty

*/


answered Feb 3, 2015 by avibootz
0 votes
#include <stdio.h>

#define SIZE 32

int main(void)
{
	char s[SIZE] = "That Code!";
      
    if (s[0] == '\0')
        printf("Empty\n");
    else
        printf("NOT Empty\n");
		
	s[0] = '\0';
	if (s[0] == '\0')
        printf("Empty\n");
    else
        printf("NOT Empty\n");
     
    return 0;
}
  
    
/*
      
run:

NOT Empty
Empty

*/

 



answered Feb 2, 2016 by avibootz
0 votes
#include <stdio.h>
#include <string.h>

#define SIZE 32

int main(void)
{
	char s[SIZE] = "";
	
	if (strcmp(s, "") == 0)
        printf("Empty\n");
    else
        printf("NOT Empty\n");
     
    return 0;
}
  
    
/*
      
run:

Empty

*/

 



answered Feb 2, 2016 by avibootz
0 votes
#include <stdio.h>

#define SIZE 32

int main(void)
{
	char s[SIZE] = "";
	
	printf("Enter a string: "); // Ctrl + c = Empty
	if (scanf("%s", s) == -1)
        printf("Empty\n");
    else
        printf("NOT Empty\n");
     
    return 0;
}
  
    
/*
      
run:

Enter a string: Empty

*/

 



answered Feb 2, 2016 by avibootz

Related questions

3 answers 207 views
1 answer 151 views
2 answers 131 views
131 views asked Feb 26, 2021 by avibootz
1 answer 125 views
125 views asked Apr 13, 2020 by avibootz
...