How can I count the number of occurrences in c of each letter (ignoring case) in the string? So that it would print out letter: # number of occurences, I have code to count the occurences of one letter, but how can I count the occurence of each letter in the string?
{
char
int count = 0;
int i;
//int length = strlen(string);
for (i = 0; i < 20; i++)
{
if (string[i] == ch)
{
count++;
}
}
return count;
}
output:
a : 1
b : 0
c : 2
etc...
Let's assume you have a system where char is eight bit and all the characters you're trying to count are encoded using a non-negative number. In this case, you can write:
const char *str = "The quick brown fox jumped over the lazy dog.";
int counts[256] = { 0 };
int i;
size_t len = strlen(str);
for (i = 0; i < len; i++) {
counts[(int)(str[i])]++;
}
for (i = 0; i < 256; i++) {
if ( count[i] != 0) {
printf("The %c. character has %d occurrences.\n", i, counts[i]);
}
}
Note that this will count all the characters in the string. If you are 100% absolutely positively sure that your string will have only letters (no numbers, no whitespace, no punctuation) inside, then 1. asking for "case insensitiveness" starts to make sense, 2. you can reduce the number of entries to the number of characters in the English alphabet (namely 26) and you can write something like this:
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
const char *str = "TheQuickBrownFoxJumpedOverTheLazyDog";
int counts[26] = { 0 };
int i;
size_t len = strlen(str);
for (i = 0; i < len; i++) {
// Just in order that we don't shout ourselves in the foot
char c = str[i];
if (!isalpha(c)) continue;
counts[(int)(tolower(c) - 'a')]++;
}
for (i = 0; i < 26; i++) {
printf("'%c' has %2d occurrences.\n", i + 'a', counts[i]);
}
Like this:
int counts[26];
memset(counts, 0, sizeof(counts));
char *p = string;
while (*p) {
counts[tolower(*p++) - 'a']++;
}
This code assumes that the string is null-terminated, and that it contains only characters a through z or A through Z, inclusive.
To understand how this works, recall that after conversion tolower each letter has a code between a and z, and that the codes are consecutive. As the result, tolower(*p) - 'a' evaluates to a number from 0 to 25, inclusive, representing the letter's sequential number in the alphabet.
This code combines ++ and *p to shorten the program.
One simple possibility would be to make an array of 26 ints, each is a count for a letter a-z:
int alphacount[26] = {0}; //[0] = 'a', [1] = 'b', etc
Then loop through the string and increment the count for each letter:
for(int i = 0; i<strlen(mystring); i++) //for the whole length of the string
if(isalpha(mystring[i]))
alphacount[tolower(mystring[i])-'a']++; //make the letter lower case (if it's not)
//then use it as an offset into the array
//and increment
It's a simple idea that works for A-Z, a-z. If you want to separate by capitals you just need to make the count 52 instead and subtract the correct ASCII offset
#include <stdio.h>
#include <string.h>
void main()
{
printf("PLEASE ENTER A STRING\n");
printf("GIVE ONLY ONE SPACE BETWEEN WORDS\n");
printf("PRESS ENETR WHEN FINISHED\n");
char str[100];
int arr[26]={0};
char ch;
int i;
gets(str);
int n=strlen(str);
for(i=0;i<n;i++)
{
ch=tolower(str[i]);
if(ch>=97 && ch<=122)
{
arr[ch-97]++;
}
}
for(i=97;i<=122;i++)
printf("%c OCCURS %d NUMBER OF TIMES\n",i,arr[i-97]);
return 0;
}
After Accept Answer
A method that meets these specs: (IMO, the other answers do not meet all)
It is practical/efficient when char has a wide range. Example: CHAR_BIT is 16 or 32, so no use of bool Used[1 << CHAR_BIT];
Works for very long strings (use size_t rather than int).
Does not rely on ASCII. ( Use Upper[] )
Defined behavior when a char < 0. is...() functions are defined for EOF and unsigned char
static const char Upper[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
static const char Lower[] = "abcdefghijklmnopqrstuvwxyz";
void LetterOccurrences(size_t *Count, const char *s) {
memset(Count, 0, sizeof *Count * 26);
while (*s) {
unsigned char ch = *s;
if (isalpha(ch)) {
const char *caseset = Upper;
char *p = strchr(caseset, ch);
if (p == NULL) {
caseset = Lower;
p = strchr(caseset, ch);
}
if (p != NULL) {
Count[p - caseset]++;
}
}
}
}
// sample usage
char *s = foo();
size_t Count[26];
LetterOccurrences(Count, s);
for (int i=0; i<26; i++)
printf("%c : %zu\n", Upper[i], Count[i]);
}
You can use the following code.
main()
{
int i = 0,j=0,count[26]={0};
char ch = 97;
char string[100]="Hello how are you buddy ?";
for (i = 0; i < 100; i++)
{
for(j=0;j<26;j++)
{
if (tolower(string[i]) == (ch+j))
{
count[j]++;
}
}
}
for(j=0;j<26;j++)
{
printf("\n%c -> %d",97+j,count[j]);
}
}
Hope this helps.
#include<stdio.h>
#include<string.h>
#define filename "somefile.txt"
int main()
{
FILE *fp;
int count[26] = {0}, i, c;
char ch;
char alpha[27] = "abcdefghijklmnopqrstuwxyz";
fp = fopen(filename,"r");
if(fp == NULL)
printf("file not found\n");
while( (ch = fgetc(fp)) != EOF) {
c = 0;
while(alpha[c] != '\0') {
if(alpha[c] == ch) {
count[c]++;
}
c++;
}
}
for(i = 0; i<26;i++) {
printf("character %c occured %d number of times\n",alpha[i], count[i]);
}
return 0;
}
for (int i=0;i<word.length();i++){
int counter=0;
for (int j=0;j<word.length();j++){
if(word.charAt(i)==word.charAt(j))
counter++;
}// inner for
JOptionPane.showMessageDialog( null,word.charAt(i)+" found "+ counter +" times");
}// outer for
#include<stdio.h>
void frequency_counter(char* str)
{
int count[256] = {0}; //partial initialization
int i;
for(i=0;str[i];i++)
count[str[i]]++;
for(i=0;str[i];i++) {
if(count[str[i]]) {
printf("%c %d \n",str[i],count[str[i]]);
count[str[i]]=0;
}
}
}
void main()
{
char str[] = "The quick brown fox jumped over the lazy dog.";
frequency_counter(str);
}
Here is the C code with User Defined Function:
/* C Program to count the frequency of characters in a given String */
#include <stdio.h>
#include <string.h>
const char letters[] = "abcdefghijklmnopqrstuvwxzy";
void find_frequency(const char *string, int *count);
int main() {
char string[100];
int count[26] = { 0 };
int i;
printf("Input a string: ");
if (!fgets(string, sizeof string, stdin))
return 1;
find_frequency(string, count);
printf("Character Counts\n");
for (i = 0; i < 26; i++) {
printf("%c\t%d\n", letters[i], count[i]);
}
return 0;
}
void find_frequency(const char *string, int *count) {
int i;
for (i = 0; string[i] != '\0'; i++) {
p = strchr(letters, string[i]);
if (p != NULL) {
count[p - letters]++;
}
}
}
Have checked that many of the answered are with static array, what if suppose I have special character in the string and want a solution with dynamic concept. There can be many other possible solutions, it is one of them.
here is the solutions with the Linked List.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct Node {
char data;
int counter;
struct Node* next;
};
void printLinkList(struct Node* head)
{
while (head != NULL) {
printf("\n%c occur %d", head->data, head->counter);
head = head->next;
}
}
int main(void) {
char *str = "!count all the occurances of character in string!";
int i = 0;
char tempChar;
struct Node* head = NULL;
struct Node* node = NULL;
struct Node* first = NULL;
for(i = 0; i < strlen(str); i++)
{
tempChar = str[i];
head = first;
if(head == NULL)
{
node = (struct Node*)malloc(sizeof(struct Node));
node->data = tempChar;
node->counter = 1;
node->next = NULL;
if(first == NULL)
{
first = node;
}
}
else
{
while (head->next != NULL) {
if(head->data == tempChar)
{
head->counter = head->counter + 1;
break;
}
head = head->next;
}
if(head->next == NULL)
{
if(head->data == tempChar)
{
head->counter = head->counter + 1;
}
else
{
node = (struct Node*)malloc(sizeof(struct Node));
node->data = tempChar;
node->counter = 1;
node->next = NULL;
head->next = node;
}
}
}
}
printLinkList(first);
return 0;
}
int charset[256] = {0};
int charcount[256] = {0};
for (i = 0; i < 20; i++)
{
for(int c = 0; c < 256; c++)
{
if(string[i] == charset[c])
{
charcount[c]++;
}
}
}
charcount will store the occurence of any character in the string.
//This is JavaScript Code.
function countWordOccurences()
{
// You can use array of words or a sentence split with space.
var sentence = "The quick brown fox jumped over the lazy dog.";
//var sentenceArray = ['asdf', 'asdf', 'sfd', 'qwr', 'qwr'];
var sentenceArray = sentence.split(' ', 1000);
var output;
var temp;
for(var i = 0; i < sentenceArray.length; i++) {
var k = 1;
for(var j = i + 1; j < sentenceArray.length; j++) {
if(sentenceArray[i] == sentenceArray[j])
k = k + 1;
}
if(k > 1) {
i = i + 1;
output = output + ',' + k + ',' + k;
}
else
output = output + ',' + k;
}
alert(sentenceArray + '\n' + output.slice(10).split(',', 500));
}
You can see it live --> http://jsfiddle.net/rammipr/ahq8nxpf/
//c code for count the occurence of each character in a string.
void main()
{
int i,j; int c[26],count=0; char a[]="shahid";
clrscr();
for(i=0;i<26;i++)
{
count=0;
for(j=0;j<strlen(a);j++)
{
if(a[j]==97+i)
{
count++;
}
}
c[i]=count;
}
for(i=0;i<26;i++)
{
j=97+i;
if(c[i]!=0) { printf("%c of %d times\n",j,c[i]);
}
}
getch();
}
protected void btnSave_Click(object sender, EventArgs e)
{
var FullName = "stackoverflow"
char[] charArray = FullName.ToLower().ToCharArray();
Dictionary<char, int> counter = new Dictionary<char, int>();
int tempVar = 0;
foreach (var item in charArray)
{
if (counter.TryGetValue(item, out tempVar))
{
counter[item] += 1;
}
else
{
counter.Add(item, 1);
}
}
//var numberofchars = "";
foreach (KeyValuePair<char, int> item in counter)
{
if (counter.Count > 0)
{
//Label1.Text=split(item.
}
Response.Write(item.Value + " " + item.Key + "<br />");
// Label1.Text=item.Value + " " + item.Key + "<br />";
spnDisplay.InnerText= item.Value + " " + item.Key + "<br />";
}
}
Related
I have problem with my alignement. This time I want my program to return words that ends and starts with the same letter. I've wrote something like this, but it seems to return random words.
#include <stdio.h>
#include <string.h>
void main()
{
char str[100];
int i, t, j, len;
printf("Enter a string : ");
scanf("%[^\n]s", str);
len = strlen(str);
str[len] = ' ';
for (t = 0, i = 0; i < strlen(str); i++)
{
if ((str[i] == ' ') && (str[i - 1] == str[0]))
{
for (j = t; j < i; j++)
printf("%c", str[j]);
t = i + 1;
printf("\n");
}
else
{
if (str[i] == ' ')
{
t = i + 1;
}
}
}
}
You can use strtok to split the strings from stdin, then apply a letter checker on each parsed word one at a time.
Something like this:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define MAXCHAR 100
int is_start_end(char *word);
void exit_if_null(void *ptr, const char *msg);
int
main(void) {
char str[MAXCHAR];
char *word;
char **all_words;
int words_size = 1, word_count = 0;
int i, found;
all_words = malloc(words_size * sizeof(*all_words));
exit_if_null(all_words, "initial Allocation");
printf("Enter words(enter empty line to terminate):\n");
while (fgets(str, MAXCHAR, stdin) != NULL && strlen(str) != 1) {
word = strtok(str, " \n");
while (word !=NULL) {
if (words_size == word_count) {
words_size *= 2;
all_words = realloc(all_words, words_size * sizeof(*all_words));
exit_if_null(all_words, "Reallocation");
}
all_words[word_count] = malloc(strlen(word)+1);
exit_if_null(all_words[word_count], "Initial Allocation");
strcpy(all_words[word_count], word);
word_count++;
word = strtok(NULL, " \n");
}
}
printf("Words that have equal first and last letters:\n");
found = 0;
for (i = 0; i < word_count; i++) {
if (is_start_end(all_words[i])) {
found = 1;
printf("%s\n", all_words[i]);
}
free(all_words[i]);
all_words[i] = NULL;
}
if (found == 0) {
printf("None Found\n");
}
free(all_words);
all_words = NULL;
return 0;
}
int
is_start_end(char *word) {
int len;
len = strlen(word);
if ((len == 1) || (tolower(word[0]) == tolower(word[len-1]))) {
return 1;
}
return 0;
}
void
exit_if_null(void *ptr, const char *msg) {
if (!ptr) {
printf("Unexpected null pointer: %s\n", msg);
exit(EXIT_FAILURE);
}
}
This line removes the null terminator of the string:
len = strlen(str);
str[len] = ' ';
thus the string no longer exists, what is left is just an ordinary array of characters.
The next call to strlen, in the body of the for loop, will cause undefined behavior.
I have a large string char myStr="AAAABBBCCCCCCDDDEFGHHIJJ".
I shall pass this string to my string compressing function which should return me the string in below format myStr ="A4B3C6D3EFGH2IJ2"
Also, the new string replacements should happen in the same passed string only. One cannot create a temp array.
Below is my func and am not able to figure out the deletion of duplicates and replacing with its count in the same string.
#include<stdio.h>
#include<string.h>
char* StrCompress(char myStr[])
{
char *s = myStr;
int len = strlen(myStr);
char *in = myStr;
int count =0;
int i=0;
while(*(s) != '\0')
{
if(*(s)==*(s+1))
{
count++;
if(count == 1)
{
in = s;
}
s++;
}
else
{
//myStr[count-1]=count;
memcpy(in+1,s+1,count);
s=in;
count =0;
}
i++;
}
return myStr;
}
int main(){
char myStr[] ="AAAABBBCCCCCEEFGIIJJJKLMNNNNOOO";
printf("Compressed String is : %s\n",StrCompress(&myStr));
return 0;
}
A slightly modified version:
char* StrCompress(char myStr[])
{
char *s, *in;
for (s = myStr, in = myStr; *s; s++) {
int count = 1;
in[0] = s[0]; in++;
while (s[0] == s[1]) {
count++;
s++;
}
if (count > 1) {
int len = sprintf(in, "%d", count);
in += len;
}
}
in[0] = 0;
return myStr;
}
Additionally, you should not use the address of operator when calling with an array name:
StrCompress(myStr); // not StrCompress(&myStr)
If you are assuming that a character can't repeat more then 9 times, then you can use in[0] = '0' + count instead of the sprintf stuff:
if (count > 1) {
in[0] = '0' + count;
in++;
}
#include<stdio.h>
char* StrCompress(char myStr[])
{
char *s = myStr;
char *r, *p;
int count, i;
while (*s)
{
/*initially only 1 character of a kind is present*/
count = 1;
/*we check whether current character matches the next one*/
while (*s && *s == *(s+1))
{
/*if yes,then increase the count due to the match
and increment the string pointer to next */
count++;
s++;
}
if (count > 1) /*if more than one character of a kind is present*/
{
/*assign the value of count to second occurence of a particular character*/
*(s - count + 2) = count + '0';
/*delete all other occurences except the first one and second one using array shift*/
for (i = 0; i < count - 2; i++)
{
p = s + 1;
r = s;
while (*r)
*r++ = *p++;
s--;
}
}
s++;
}
return myStr;
}
int main()
{
char myStr[] = "AAAABBBCCCCCCDDDEFGHHIJJ";
printf("Compressed String is : %s\n", StrCompress(myStr));
return 0;
}
public static void main(String...args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the String:");
String str=sc.next();
int count=1;
for(int i=0;i<str.length()-1;i++) {
Character ch1=str.charAt(i);
Character ch2=str.charAt(i+1);
if(ch1.equals(ch2)) {
count++;
}
else
{
System.out.print((char)(str.charAt(i)));
if(count>1) {
System.out.print(count);
}
count=1;
}
if(i==(str.length()-2))
{
if(ch1.equals(ch2))
{System.out.print(ch1+""+count);}
else {System.out.print(ch2);}
}
}
}
public static String compress(String str) {
StringBuilder result = new StringBuilder();
int i = 0;
int count = 0;
while(i < str.length() - 1) {
count++;
if (str.charAt(i) != str.charAt(i + 1)) {
result.append(str.charAt(i)).append(count);
count = 0;
}
i++;
}
result.append(str.charAt(i)).append(count + 1);
return result.toString();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.print("enter the string");
String s=(new Scanner(System.in)).nextLine();
String s2=new String("");
int count=0;
for(int i=0;i<s.length();i++)
{
count=1;
s2=s2+(s.charAt(i));
while(i+1<s.length() && s.charAt(i+1)==s.charAt(i) )
{
count++;
i++;
}
s2=s2.concat(count+"");
}
System.out.print(s2);
}
}
Below is another implementation in case anyone needs it. FYI, this method is called run-length encoding
#include <iostream>
void CompressString (std::string str)
{
//count will keep track of the number of occurences of any given character
unsigned int count = 1;
//new string to store the values from the original string
std::string str2 = "";
//store the first letter of the string initially
char ch = str[0];
//run a loop from the second character of the string since first character if stored in "ch"
for (unsigned int i = 1; i < str.length(); i++)
{
if (str[i] == ch)
count++;
else
{
str2 = str2 + ch + std::to_string (count);
ch = str[i];
count = 1;
}
}
//for cases like aabbb
str2 = str2 + ch + std::to_string (count);
//check if after compression, the length of the string reduces or not
if (str.length() > str2.length())
std::cout << str2 << std::endl;
else
std::cout << str << std::endl;
}
int main ()
{
std::cout << "Enter a string to compress: ";
std::string str;
getline (std::cin, str);
std::cout << "Compressed string is: ";
CompressString (str);
return 0;
}
Here is another inplace java program . We can use StringBuilder instead of string
public static void main(String[] args) {
String a = "aaabbccaaaddj";
for(int i=0;i<a.length();i++){
int c=i+1;
int duplicateCharCount=1;
while(c<a.length()&&a.charAt(c)==a.charAt(i)){
++c;
++duplicateCharCount;
}
a=a.substring(0,i+1)+duplicateCharCount+a.substring(i+duplicateCharCount);
i++;
}
System.out.println(a);
}
public class StringCompression {
public static String compress(String str) {
StringBuilder result = new StringBuilder();
int i;
int count = 0;
for(i=0; i< str.length() - 1;i++,count++) {
if (str.charAt(i) != str.charAt(i + 1)) {
result.append(str.charAt(i)).append(count);
count = 0;
}
}
result.append(str.charAt(i)).append(count);
return result.toString();
}
public static void main(String[] args) {
String string = "aaassssdddaaaggghhhfgreeeeeeedrrrrr";
String x= compress(string);
System.err.println(x);
}
}
#include<stdio.h>
#include<conio.h>
char* compress(char* str);
int main(){
clrscr();
char str[1000];
scanf("%[^\n]s", str);
char* s = compress(str);
printf("\n%s", s);
getch();
return 0;
}
char* compress(char* str){
char* s = str;
int count = 1;
char str2[1000] = "\0";
char* n = str2;
while(*(s) != '\0'){
if(count == 1){
*n = *s;
n++;
}
if(*(s) == *(s+1)){
count++;
s++;
}
else{
*n = '0' + count;
n++;
count = 1;
s++;
}
}
return str2;
}
Here's another solution with ES6:
// aaeezaa : a4e2z1
function compressString(str) {
const obj = {};
const sortedArr = [...str].sort();
for(i = 0; i<sortedArr.length; i++) {
let c = 1;
while((sortedArr[i] === sortedArr[i+1]) && sortedArr[i+1]) {
c++;
i++;
}
obj[sortedArr[i]] = c;
}
return Object.keys(obj).reduce((compressedStr, k) => compressedStr + k + obj[k], '');
}
I made two assumptions and wrote this code,
we have space double the size of string which we are encoding. i.e., suppose we are encoding "ab", then the space space allotted should be at-least 4 bytes.
continuous streak of alphabets can be max 999. if there is a chance that there can be 1000 same characters in adjacent positions then, we have to increase "count_str" char array size accordingly.
#include <stdio.h>
#include <string.h>
char *compress(char *input) {
int i = 0;
int count = 1;
int k = 0;
int j = 0;
int len = 0;
int digits_in_count = 0;
char count_str[3];
int m = 0;
for(i = 0; i < strlen(input); i++) {
j = i+1;
m = 0;
count = 1;
len = strlen(input);
printf("\niteration: %d, string = %s",i, input);
while((input[j] != '\0') && (input[j] == input[i])) {
count++;
j++;
}
sprintf(count_str, "%d", count);
digits_in_count = strlen(count_str);
//this means we have reaced last alphabet in the string
if(input[j] == '\0' && count == 1) {
k = k+1;
goto count_append;
}
input[k++] = input[i];
// we are assuming that we have enough space in the end, to move string.
// we are memmove for remaining portion of the string.
// if the string is "aaab", then we have to move 'b' one step ahead
// and it will look like "aab", later in the end we are adding count,
// and making it as "a3b".
// if the string is "ab", then we have to move 'b' one step away,
// to make space for adding 'count'.
// and the new string after memmove will looklike "abb",
// in the end we are adding count and making it as "a1b"
// memmove will not hit for last character in the string, because there
// is already enough space for appending 'count'.
memmove((input+i+digits_in_count+1) , input+j, len-j+1);
i = i+digits_in_count;
count_append:
{
while(digits_in_count) {
input[k++] = *(count_str+m);
m = m+1;
digits_in_count--;
}
}
}
return input;
}
void main()
{
char arr[50] = "aaab";
printf("\n%s\n", compress(arr));
}
void gen_compressed_str(string str){
int len = str.length();
for (int i = 0; i < len; i++) {
int count = 1;
while (i < len - 1 && str[i] == str[i + 1]) {
count++;
i++;
}
if (count == 1){
cout << str[i];
}
else{
cout << str[i]<<count;
}
}
cout<<endl;
}
void stringCompression(char a[]) {
int i, count=1,j=0;
for(i=0;a[i]!='\0';i++){
if(a[i]==a[i+1]){
count++;
}
else if(a[i]!=a[i+1]){
if(count>1){
a[j++]=a[i];
a[j++]=(char)(48+count);
}
else if(count==1){
a[j++]=a[i];
}
count=1;
}
}
a[j]='\0';
}
I'm trying to check whether a string contains a substring in C like:
char *sent = "this is my sample example";
char *word = "sample";
if (/* sentence contains word */) {
/* .. */
}
What is something to use instead of string::find in C++?
if (strstr(sent, word) != NULL) {
/* ... */
}
Note that strstr returns a pointer to the start of the word in sent if the word word is found.
Use strstr for this.
https://cplusplus.com/reference/cstring/strstr
So, you'd write it like..
char *sent = "this is my sample example";
char *word = "sample";
char *pch = strstr(sent, word);
if(pch)
{
...
}
Try to use pointers...
#include <stdio.h>
#include <string.h>
int main()
{
char str[] = "String1 subString1 Strinstrnd subStr ing1subString";
char sub[] = "subString";
char *p1, *p2, *p3;
int i=0,j=0,flag=0;
p1 = str;
p2 = sub;
for(i = 0; i<strlen(str); i++)
{
if(*p1 == *p2)
{
p3 = p1;
for(j = 0;j<strlen(sub);j++)
{
if(*p3 == *p2)
{
p3++;p2++;
}
else
break;
}
p2 = sub;
if(j == strlen(sub))
{
flag = 1;
printf("\nSubstring found at index : %d\n",i);
}
}
p1++;
}
if(flag==0)
{
printf("Substring NOT found");
}
return (0);
}
You can try this one for both finding the presence of the substring and to extract and print it:
#include <stdio.h>
#include <string.h>
int main(void)
{
char mainstring[]="The quick brown fox jumps over the lazy dog";
char substring[20], *ret;
int i=0;
puts("enter the sub string to find");
fgets(substring, sizeof(substring), stdin);
substring[strlen(substring)-1]='\0';
ret=strstr(mainstring,substring);
if(strcmp((ret=strstr(mainstring,substring)),substring))
{
printf("substring is present\t");
}
printf("and the sub string is:::");
for(i=0;i<strlen(substring);i++)
{
printf("%c",*(ret+i));
}
puts("\n");
return 0;
}
And here is how to report the position of the first character off the found substring:
Replace this line in the above code:
printf("%s",substring,"\n");
with:
printf("substring %s was found at position %d \n", substring,((int) (substring - mainstring)));
My own humble (case sensitive) solution:
uint8_t strContains(char* string, char* toFind)
{
uint8_t slen = strlen(string);
uint8_t tFlen = strlen(toFind);
uint8_t found = 0;
if( slen >= tFlen )
{
for(uint8_t s=0, t=0; s<slen; s++)
{
do{
if( string[s] == toFind[t] )
{
if( ++found == tFlen ) return 1;
s++;
t++;
}
else { s -= found; found=0; t=0; }
}while(found);
}
return 0;
}
else return -1;
}
Results
strContains("this is my sample example", "th") // 1
strContains("this is my sample example", "sample") // 1
strContains("this is my sample example", "xam") // 1
strContains("this is my sample example", "ple") // 1
strContains("this is my sample example", "ssample") // 0
strContains("this is my sample example", "samplee") // 0
strContains("this is my sample example", "") // 0
strContains("str", "longer sentence") // -1
strContains("ssssssample", "sample") // 1
strContains("sample", "sample") // 1
Tested on ATmega328P (avr8-gnu-toolchain-3.5.4.1709) ;)
This code implements the logic of how search works (one of the ways) without using any ready-made function:
public int findSubString(char[] original, char[] searchString)
{
int returnCode = 0; //0-not found, -1 -error in imput, 1-found
int counter = 0;
int ctr = 0;
if (original.Length < 1 || (original.Length)<searchString.Length || searchString.Length<1)
{
returnCode = -1;
}
while (ctr <= (original.Length - searchString.Length) && searchString.Length > 0)
{
if ((original[ctr]) == searchString[0])
{
counter = 0;
for (int count = ctr; count < (ctr + searchString.Length); count++)
{
if (original[count] == searchString[counter])
{
counter++;
}
else
{
counter = 0;
break;
}
}
if (counter == (searchString.Length))
{
returnCode = 1;
}
}
ctr++;
}
return returnCode;
}
I believe that I have the simplest answer. You don't need the string.h library in this program, nor the stdbool.h library. Simply using pointers and pointer arithmetic will help you become a better C programmer.
Simply return 0 for False (no substring found), or 1 for True (yes, a substring "sub" is found within the overall string "str"):
#include <stdlib.h>
int is_substr(char *str, char *sub)
{
int num_matches = 0;
int sub_size = 0;
// If there are as many matches as there are characters in sub, then a substring exists.
while (*sub != '\0') {
sub_size++;
sub++;
}
sub = sub - sub_size; // Reset pointer to original place.
while (*str != '\0') {
while (*sub == *str && *sub != '\0') {
num_matches++;
sub++;
str++;
}
if (num_matches == sub_size) {
return 1;
}
num_matches = 0; // Reset counter to 0 whenever a difference is found.
str++;
}
return 0;
}
Using C - No built in functions
string_contains() does all the heavy lifting and returns 1 based index. Rest are driver and helper codes.
Assign a pointer to the main string and the substring, increment substring pointer when matching, stop looping when substring pointer is equal to substring length.
read_line() - A little bonus code for reading the user input without predefining the size of input user should provide.
#include <stdio.h>
#include <stdlib.h>
int string_len(char * string){
int len = 0;
while(*string!='\0'){
len++;
string++;
}
return len;
}
int string_contains(char *string, char *substring){
int start_index = 0;
int string_index=0, substring_index=0;
int substring_len =string_len(substring);
int s_len = string_len(string);
while(substring_index<substring_len && string_index<s_len){
if(*(string+string_index)==*(substring+substring_index)){
substring_index++;
}
string_index++;
if(substring_index==substring_len){
return string_index-substring_len+1;
}
}
return 0;
}
#define INPUT_BUFFER 64
char *read_line(){
int buffer_len = INPUT_BUFFER;
char *input = malloc(buffer_len*sizeof(char));
int c, count=0;
while(1){
c = getchar();
if(c==EOF||c=='\n'){
input[count]='\0';
return input;
}else{
input[count]=c;
count++;
}
if(count==buffer_len){
buffer_len+=INPUT_BUFFER;
input = realloc(input, buffer_len*sizeof(char));
}
}
}
int main(void) {
while(1){
printf("\nEnter the string: ");
char *string = read_line();
printf("Enter the sub-string: ");
char *substring = read_line();
int position = string_contains(string,substring);
if(position){
printf("Found at position: %d\n", position);
}else{
printf("Not Found\n");
}
}
return 0;
}
The same will be achieved with this simpler code: Why use these:
int main(void)
{
char mainstring[]="The quick brown fox jumps over the lazy dog";
char substring[20];
int i=0;
puts("enter the sub string to find");
fgets(substring, sizeof(substring), stdin);
substring[strlen(substring)-1]='\0';
if (strstr(mainstring,substring))
{
printf("substring is present\t");
}
printf("and the sub string is:::");
printf("%s",substring,"\n");
return 0;
}
But the tricky part would be to report at which position in the original string the substring starts...
My code to find out if substring is exist in string or not
// input ( first line -->> string , 2nd lin ->>> no. of queries for substring
following n lines -->> string to check if substring or not..
#include <stdio.h>
int len,len1;
int isSubstring(char *s, char *sub,int i,int j)
{
int ans =0;
for(;i<len,j<len1;i++,j++)
{
if(s[i] != sub[j])
{
ans =1;
break;
}
}
if(j == len1 && ans ==0)
{
return 1;
}
else if(ans==1)
return 0;
return 0;
}
int main(){
char s[100001];
char sub[100001];
scanf("%s", &s);// Reading input from STDIN
int no;
scanf("%d",&no);
int i ,j;
i=0;
j=0;
int ans =0;
len = strlen(s);
while(no--)
{
i=0;
j=0;
ans=0;
scanf("%s",&sub);
len1=strlen(sub);
int value;
for(i=0;i<len;i++)
{
if(s[i]==sub[j])
{
value = isSubstring(s,sub,i,j);
if(value)
{
printf("Yes\n");
ans = 1;
break;
}
}
}
if(ans==0)
printf("No\n");
}
}
#include <stdio.h>
#include <string.h>
int findSubstr(char *inpText, char *pattern);
int main()
{
printf("Hello, World!\n");
char *Text = "This is my sample program";
char *pattern = "sample";
int pos = findSubstr(Text, pattern);
if (pos > -1) {
printf("Found the substring at position %d \n", pos);
}
else
printf("No match found \n");
return 0;
}
int findSubstr(char *inpText, char *pattern) {
int inplen = strlen(inpText);
while (inpText != NULL) {
char *remTxt = inpText;
char *remPat = pattern;
if (strlen(remTxt) < strlen(remPat)) {
/* printf ("length issue remTxt %s \nremPath %s \n", remTxt, remPat); */
return -1;
}
while (*remTxt++ == *remPat++) {
printf("remTxt %s \nremPath %s \n", remTxt, remPat);
if (*remPat == '\0') {
printf ("match found \n");
return inplen - strlen(inpText+1);
}
if (remTxt == NULL) {
return -1;
}
}
remPat = pattern;
inpText++;
}
}
#include <stdio.h>
int main(void)
{
int i,j;
int wordstart = -1;
int wordend = -1;
char words[]= "this is a test";
char temp;
// Reverse each word
for (i = 0; i < strlen(words); ++i)
{
wordstart = -1;
wordend = -1;
if(words[i] != ' ')
wordstart = i;
for (j = wordstart; j < strlen(words); ++j)
{
if(words[j] == ' ')
{
wordend = j - 1;
break;
}
}
if(wordend == -1)
wordend = strlen(words);
for (j = wordstart ; j <= (wordend - wordstart) / 2; ++j)
{
temp = words[j];
words[j] = words[wordend - (j - wordstart)];
words[wordend - (j - wordstart)] = temp;
}
i = wordend;
printf("reversed string is %s:", words);
}
}
I tried in this way but i am getting this output:
siht is a test
my expected output is:
test a is this
I would appreciate if some one could come with a different approach for which time complexity is very less or correct me if it is the right approach. Thanks
Perhaps this belongs on the code review site instead?
Your approach seems very efficient to me (except that I would only call strlen(words) once and save the result in a register).
Two possible bugs look like:
wordend = strlen(words);
should be
wordend = strlen(words)-1;
and
for(j = wordstart ; j <= (wordend - wordstart) / 2 ; ++j) {
should be
for(j = wordstart ; j <= (wordend + wordstart) / 2 ; ++j) {
Final code looks like (with some extra {}):
#include <stdio.h>
int main(int argc,char *argv[])
{
int i,j;
char words[]= "this is a test";
int L=strlen(words);
// Reverse each word
for(i = 0; i < L; ++i) {
int wordstart = -1;
int wordend = -1;
if(words[i] != ' ')
{
wordstart = i;
for(j = wordstart; j < L; ++j) {
if(words[j] == ' ') {
wordend = j - 1;
break;
}
}
if(wordend == -1)
wordend = L-1;
for(j = wordstart ; j <= (wordend + wordstart) / 2 ; ++j) {
char temp = words[j];
words[j] = words[wordend - (j - wordstart)];
words[wordend - (j - wordstart)] = temp;
}
i = wordend;
}
}
printf("reversed string is %s:",words);
return 0;
}
You can create a double linked list as a base data structure. Then, iterate through the words and insert them in the list as you find them.
When you reach the end of the sentence, simply traverse the list backwards and print the words as you go through them
Simply we can just use a n*1 2D character array tailored to suit our needs!!!
#include <stdlib.h>
int main()
{
char s[20][20];
int i=0, length=-1;
for(i=0;;i++)
{
scanf("%s",s[i]);
length++;
if(getchar()=='\n')
break;
}
for(i=length;i>=0;i--)
printf("%s ",s[i]);
return 0;
}
Start tokenizing the line from the last character and continue to the first character. Keep one pointer anchored at the base of the current word, and another pointed which will decrease while a word start is not found. When you find a word start while scanning like this, print from the word start pointer to the word end anchor. Update the word end anchor to the previous character of the current word start char.
You might want to skip the blankspace characters while scanning.
UPDATE
This is a quick implementation:
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#define MAX_BUF 256
void show_string (char *str, int i, int n)
{
while (i <= n)
{
printf ("%c", str[i]);
i++;
}
}
int main (void)
{
char str[MAX_BUF];
int end_anchor, start_ptr;
int state;
printf ("\nEnter a string: ");
scanf (" %[^\n]", str);
start_ptr = strlen (str) - 1;
end_anchor = start_ptr;
state = 0;
while (start_ptr >= -1)
{
switch (state)
{
case 0:
if ((!isspace (str[start_ptr]) && (start_ptr >= 0)))
{
start_ptr--;
}
else
{
state = 1;
}
break;
case 1:
show_string (str, start_ptr + 1, end_anchor);
state = 2;
start_ptr--;
printf (" ");
break;
case 2:
if (!isspace (str[start_ptr]))
{
state = 0;
end_anchor = start_ptr;
}
else
{
start_ptr--;
}
break;
}
}
printf ("\n");
return 0;
}
The end_anchor points to each end word, and the start_ptr finds the start of the word of which the end is held by end_anchor. When we find a word start (by blankspace characters or start_ptr = -1), we print all the characters from start_ptr + 1 to end_anchor. The + 1 is because of the implementation: start_ptr points to the blankspace character, and the print routine will print all the characters from i to n. Once we have detected one blank space we print it and we skip adjacent blankspaces (in case 2) and preserve only one which is manually printed. Once a non blankspace is detected, we have got another word end, for which we set the end_anchor to this index in the case 2, and set state = 0 , so that we can search for the word start again.
if(words[i] != ' ')
wordstart = i;
This statement what about the else part? if words[i] == ' ', and wordstart remains -1.
So maybe try to use:
while (words[i] && words[i] == ' ') ++i;
if (!words[i])
break;
wordstart = i;
Then you should output the result out of the i loop.
Finally, if you want to get the result you expected, you should reverse the whole sentence once more, with the way you used in the loop.
I would use write function similar to strrchr for finding last occurence of ' ', if its found print word that follows, rewrite this ' ' with '\0' and repeat it in loop till no more words are found. At the end I would print the content of this string again because there is most likely no ' ' before the first word.
I would write own function instead of strrchr because strrchr calculates the lenght of the given string, which is redundant in this case. This length doesn't have to be calculated more than once.
Here's the code:
char* findLastWord(char* str, int* len)
{
int i;
for (i = *len - 1; i >= 0; --i)
{
if (str[i] == ' ')
{
str[i] = '\0';
if (i < *len - 1)
{
*len = i - 1;
return &str[i + 1];
}
}
}
return NULL;
}
int main (int argc, char *argv[])
{
char str[] = " one two three four five six ";
int len = strlen(str);
char* lastWord = findLastWord(str, &len);
while (lastWord != NULL)
{
printf("%s\n", lastWord);
lastWord = findLastWord(str, &len);
}
if (len > 1)
printf("%s\n", str);
return 0;
}
output:
six
five
four
three
two
one
Hope this helps ;)
#include<stdio.h>
#include<string.h>
void reverse(char *str, size_t len)
{
char tmp;
size_t beg, end;
if (len <=1) return;
for (beg=0,end=len; beg < --end ; beg++) {
tmp = str[beg];
str[beg] = str[end];
str[end] = tmp;
}
}
int main(void)
{
char sentence[] = "one two three four five";
size_t pos, len;
printf("Before:%s\n",sentence);
for (pos = len= 0; sentence[pos]; pos += len) {
pos += strspn( sentence+pos, " \t\n" );
len = strcspn( sentence+pos, " \t\n" );
reverse ( sentence + pos, len );
}
reverse ( sentence , pos );
printf("After:%s\n",sentence);
return 0;
}
#include <iostream>
#include <string>
using namespace std;
char* stringrev(char s[], int len)
{
char *s1 = (char*)malloc(len+1);
int i=0;
while (len>0)
{
s1[i++] = s[--len];
}
s1[i++] = '\0';
return s1;
}
void sentrev(char s[], int len)
{
int i=0; int j=0;
char *r = (char*)malloc(len+1);
while(1)
{
if(s[j] == ' ' || s[j] == '\0')
{
r = stringrev(s+i, j-i);
i = j+1;
cout<<r<<" ";
}
if (s[j] == '\0')
break;
j++;
}
}
int main()
{
char *s = "this is a test";
char *r = NULL;
int len = strlen(s);
cout<<len<<endl;
r = stringrev(s, len);
cout<<r<<endl;
sentrev(r, len);
return 0;
}
The above code snap reverse the sentence, using char *r
and printing cout<
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
char st[50], rst[50];
printf("Enter the sentence...\n");
gets(st);
int len=strlen(st), p;
int j=-1,k;
p=len;
for(int i=(len-1); i>=0; i--)
{
//searching for space or beginning
if(st[i]==' ')
{
//reversing and storing each word except the first word
for(k=i+1;k<p;k++)
{
//printf("%c",st[k]);
rst[++j]=st[k];
}
j++;
rst[j]=' ';
printf("\n");
p=i;
}
else if(i==0)
{
//for first word
for(k=i;k<p;k++)
{
//printf("%c",st[k]);
rst[++j]=st[k];
}
}
}
printf("Now reversing the sentence...\n");
puts(rst);
return 0;
}
Use a main for loop to traverse till the end of the sentence:
Copy the letters in a string until you find a space.
now call add#beginning function and in that function add the string each time you pass a string to the linked list.
print the contents of the linked list with a space inbetween to get the expected output
My code,just traverse from the last and if you find a space print the characters before it,now change the end to space-1;This will print till the second word,finally just print the first word using a single for loop.Comment for alter approach.
Program:
#include<stdio.h>
int main()
{
char str[200];
int i,j,k;
scanf("%[^\n]s",&str);
for(i=0;str[i]!='\0';i++);
i=i-1;
for(j=i;j>=0;j--)
{
if((str[j])==' ')
{
for(k=j+1;k<=i;k++)
{
printf("%c",str[k]);
}
i=j-1;
printf(" ");
}
}
for(k=0;k<=i;k++)
{
printf("%c",str[k]);
}
}
using stack
#include <iostream>
#include <stdio.h>
#include <stack>
int main()
{
std::stack<string> st;
char *words= "this is a test";
char * temp = (char *)calloc(1, sizeof(*temp));
int size1= strlen(words);
int k2=0;
int k3=0;
for(int i=0;i<=size1;i++)
{
temp[k2] = words[i];
k2++;
if(words[i] == ' ')
{
k3++;
if(k3==1)
temp[k2-1]='\0';
temp[k2]='\0';
st.push(temp);
k2=0;
}
if(words[i] == '\0')
{
temp[k2]='\0';
st.push(temp);
k2=0;
break;
}
}
while (!st.empty())
{
printf("%s",st.top().c_str());
st.pop();
}
I am trying to make "Hello World" to "World Hello".
But the code is not working properly the way I wanted it to behave.
See the code below:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct llnode
{
char *info;
struct llnode *next;
};
typedef struct llnode NODE;
int main()
{
char msg[50],word[10],*str;
int i=0,length=0,j=0;
NODE *ptr,*front=NULL,*temp,*last=NULL;
//printf("Enter the sentence: ");
str= "Hello World"; //fgets(msg,sizeof(msg),stdin);
while(str[i]!='\0')
{
if((str[i]==' ')||(str[i]=='\n'))
{
word[j]='\0';
j=0;
ptr=(NODE *)malloc(sizeof(NODE));
ptr->info=word;
ptr->next=NULL;
if(front==NULL)
{
front=ptr; // only change the value of front here;
}
else
{
temp=front;
while((temp->next)!=NULL)
{
temp=temp->next;
}
temp->next=ptr;
}
printf("\n##%s\n",front->info); // prints thewords and not
//the first word
}
else
{
word[j]=str[i];
j++;
}
i++;
}
temp=front;
while(temp)
{
length++;
printf("%s ",temp->info);
temp=temp->next;
}
printf("\nLength of Linked List(or, number of words): %d\n",length);
i=0;
printf("\n************************\n");
while(i<length)
{
temp=front;
while(temp->next!=last)
{
temp=temp->next;
}
last=temp;
printf("%s ",temp->info);
i++;
}
return 0;
}
Thanks
There are a number of things wrong with the code:
You are using a single word array to read all the words. So, when you read "Hello", you read into the word array, print "##Hello" and store the pointer to the word array as front->info. Then, you OVERWRITE the word array with World. Also, please note that you NEVER add a node with the word "World" because you exit the loop as soon as you encounter the '\0'. So, your linked list contains only one node. But, there is a problem, since you stored a pointer to the word array in the first node and since the word array has been overwritten with "World", when you exit the loop, there is only one node in the list and the info of this node is word array which contains "World" and not "Hello" like it once did. So, I guess this explains the output?
You should be able to use strtok() for this purpose. See this example, just replace the hashtags with spaces and print backwards. This is by far the easiest way to accomplish this.
Looks like homework... but, for starters, if your delimiters are a space and a newline:
if((str[i]==' ')||(str[i]=='\n'))
...then a string that doesn't contain a space or a newline at the end will never parse the last element:
str= "Hello World"; //fgets(msg,sizeof(msg),stdin);
...so my guess is that you're never even putting "World" into the linked list.
Finally I did this one
/**
I am a boy -> boy a am I
*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
int i, j, n, temp, temp_i, cnt;
//char *array = "Samsung";
char array[1000];
char newarr[strlen(array)];
printf("Enter The String: \n");
gets(array);
for(i = (strlen(array)-1), n = 0, j = 0; i >= 0; i--)
{
if( array[i] != ' ')
{
n++;
}
else
{
temp = n;
temp_i = i;
for(n = 0; n <= temp; n++)
{
// i = i + 1;
newarr[j++] = array[i++];
}
i = temp_i;
n = 0;
}
if(i == 0)
{
newarr[j++] = ' ';
temp = n;
temp_i = i;
for(n = 0; n <= temp; n++)
{
// i = i + 1;
newarr[j++] = array[i++];
}
i = temp_i;
n = 0;
}
//newarr[j++] = array[i];
}
newarr[j] = '\0';
cnt = 0;
for(j = 0; j <= (strlen(newarr)-1); j++)/*This is not required just do some R n D*/
{
newarr[j] = newarr[++cnt];
}
// printf("The first element is %c \n", newarr[1]);
puts(newarr);
return 0;
}
Here is one solution with c++11 . Which reverses the words as required and prints it on screen.
vector<string> words;
string str = "hello world c++11";
size_t current = 0;
size_t found = str.find(" ");
while(found != string::npos)
{
words.push_back(str.substr(current, found - current));
current = found + 1;
found = str.find(" ",current);
}
words.push_back(str.substr(current));
std::ostream_iterator<string> Display_iter(std::cout," ") ;
std::copy(words.rbegin(), words.rend(), Display_iter);
1) First reverse the entire string ( it gives like "dlrow olleh")
2) and then call/reverse word from first character until space/endOfString encounters.
3) It gives desired output.
#include
#include
int main() {
char *src = "I am a boy";
char dest[50][50];
int idx = 0;
int priv_idx = 0;
int i = 0;
int j = 0;
while(src[i] != '\0') {
if(src[i] == ' ') {
if(priv_idx == idx) {
idx ++;
j = 0;
}
i++;
continue;
}
*(*(dest + idx) + j) = src[i];
i++;
j++;
priv_idx = idx;
}
for (i = idx; i>=0; --i) {
printf("%s\n\r",dest[i]);
}
return 0;
}
#include <stdio.h>
#include <string.h>
#define MAX_ROW 50
#define MAX_COLUMN 50
char dest[MAX_ROW][MAX_COLUMN];
int str_rev_order(char *src)
{
int idx = 0;
int priv_idx = 0;
int i = 0;
int j = 0;
for(i = 0;i<MAX_ROW; ++i) {
memset(dest[i],0,MAX_COLUMN);
}
/* reset the counter */
i = 0;
while(src[i] != '\0') {
if(idx >= MAX_ROW-1) {
printf("Don't support more than %d substring.\n\r",MAX_ROW);
return -1;
}
if(j >= MAX_COLUMN -1) {
printf("Don't support string length more than %d.\n\r",MAX_COLUMN);
return -1;
}
if(src[i] == ' ') {
if(priv_idx == idx) {
/* going to next row & reset the column counter*/
idx ++;
j = 0;
}
i++;
continue;
}
*(*(dest + idx) + j) = src[i];
i++;
j++;
priv_idx = idx;
}
return idx;
}
void print_rev_order(int idx) {
int i;
for (i = idx; i>=0; --i) {
printf("%s\n\r",dest[i]);
}
}
int main() {
char *src = "I am a boy";
int idx = str_rev_order(src);
print_rev_order(idx);
return 0;
}