PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0
Showing posts with label editing. Show all posts
Showing posts with label editing. Show all posts

Sunday, July 17, 2022

[FIXED] how do i find on what place a certain letter is in a sentence? (c programming)

 July 17, 2022     arrays, c, editing, text, warnings     No comments   

Issue

I'm trying to find to find which position a number has in a sentence in c. I'm kinda new to programming and i don't know why my code isn't working.

i keep getting this warning but i have no clue what it means (english isn't my first language):

passing argument 1 of 'strcmp' makes pointer from integer without a cast [-Wint-conversion] Main.c /TweeIntegers line 20 C/C++ Problem

My code:

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

int main()
{
    int i, y;
    char x;

    char text1[] = "een stuk text";
    char text2[] = "k";

    for ( i = 0; i < strlen(text1); i++ )
    {
        x = text1[i];
        y = strcmp( x, text2 )
    }

    printf("%d", i);

    return 0;
}

Solution

if you are only looking for a char and the first position then you can use the following code:

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

int main()
{
  int i;

  char text1[] = "een stuk text";
  char charYouLookFor = 'k';

  for ( i = 0; i < strlen(text1); i++ )
  {
    if (text1[i] == charYouLookFor)
       break;
  }

  printf("%d", i);

  return 0;
}

If you are looking for the position of a text in a text or for the second position of the char the code needs to be more complex.



Answered By - Ludger H.
Answer Checked By - Cary Denson (PHPFixing Admin)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Older Posts Home

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
All Comments
Atom
All Comments

Copyright © PHPFixing