+ 1
Find the First Non-Repeating Character in a string using pointer
Hi viewers, could you come up with the program using C language?
6 Antworten
+ 5
The code you give had some logical errors, syntax errors, had some issues on pointer increment, null checkers and issues in format specificers as well. I suggest you to know the basic of C programming language in deeply to overcome this type of errors. As a suggestion, try to create a code snippet from code bits don't copy and paste here directly please.
Below, is your corrected code. And check the comments where I fixing your code. Have fun!
https://sololearn.com/compiler-playground/cGygN46bze5Y/?ref=app
+ 4
Three things to get answers for a problem solving questions;
1️⃣ Show you effort ( code )
2️⃣ Add a little description of your purpose/effort
3️⃣ Avoid sharing as a Homework or Task
+ 2
We can help you write the code, but writing it all by ourselves is not helping.
Please show us your code attempt and we will help you understand how to write the rest/debug it.
+ 1
#include <stdio.h>
char firstNonRepeatingChar(char *str) {
char *p = str;
char *q;
int count;
while (*p != '\0') {
q = str;
count = 0;
while (*q != 0); {
if (*p = *q)
count++;
q++;
}
if (count = 1)
return *p;
*p++;
}
return 0;
}
int main() {
char str[] = "swiss";
char result;
result = firstNonRepeatingChar(str);
if (result != 0)
printf("First non-repeating character is: %s\n", result);
else
printf("No unique character found\n");
return 0;
}
+ 1
Thankyou
+ 1
You can solve this problem using a two-pass approach. First, you iterate through the string to count the frequency of each character. Then, in the second pass, you check each character in the string to see if its count is 1 (meaning it’s non-repeating). Once you find a character with a count of 1, you return it. This approach is efficient and works well with pointers since you can traverse the string directly without needing array indexing. Let me know if you need more details on how to implement this!